blob: 17f2c2293f8ea124aeb9f24cd189d023d05039b4 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
4/* Copyright (C) 2018, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030029
itayzafrir7723ab12019-02-14 10:28:02 +020030#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010031#include "psa/crypto.h"
32
Gilles Peskine039b90c2018-12-07 18:24:41 +010033#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010034#include "psa_crypto_invasive.h"
Gilles Peskine961849f2018-11-30 18:54:54 +010035#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010036/* Include internal declarations that are useful for implementing persistently
37 * stored keys. */
38#include "psa_crypto_storage.h"
39
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010040#include <stdlib.h>
41#include <string.h>
42#if defined(MBEDTLS_PLATFORM_C)
43#include "mbedtls/platform.h"
44#else
45#define mbedtls_calloc calloc
46#define mbedtls_free free
47#endif
48
Gilles Peskinea5905292018-02-07 20:59:33 +010049#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020050#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000051#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020052#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010053#include "mbedtls/blowfish.h"
54#include "mbedtls/camellia.h"
55#include "mbedtls/cipher.h"
56#include "mbedtls/ccm.h"
57#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010058#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010059#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020060#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010061#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010062#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010063#include "mbedtls/error.h"
64#include "mbedtls/gcm.h"
65#include "mbedtls/md2.h"
66#include "mbedtls/md4.h"
67#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010068#include "mbedtls/md.h"
69#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010070#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010071#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010072#include "mbedtls/platform_util.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010073#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010074#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010075#include "mbedtls/sha1.h"
76#include "mbedtls/sha256.h"
77#include "mbedtls/sha512.h"
78#include "mbedtls/xtea.h"
79
Gilles Peskine996deb12018-08-01 15:45:45 +020080#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
81
Gilles Peskine9ef733f2018-02-07 21:05:37 +010082/* constant-time buffer comparison */
83static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
84{
85 size_t i;
86 unsigned char diff = 0;
87
88 for( i = 0; i < n; i++ )
89 diff |= a[i] ^ b[i];
90
91 return( diff );
92}
93
94
95
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010096/****************************************************************/
97/* Global data, support functions and library management */
98/****************************************************************/
99
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200100static int key_type_is_raw_bytes( psa_key_type_t type )
101{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200102 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200103}
104
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100105/* Values for psa_global_data_t::rng_state */
106#define RNG_NOT_INITIALIZED 0
107#define RNG_INITIALIZED 1
108#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100109
Gilles Peskine2d277862018-06-18 15:41:12 +0200110typedef struct
111{
Gilles Peskine5e769522018-11-20 21:59:56 +0100112 void (* entropy_init )( mbedtls_entropy_context *ctx );
113 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100114 mbedtls_entropy_context entropy;
115 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100116 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100117 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100118} psa_global_data_t;
119
120static psa_global_data_t global_data;
121
itayzafrir0adf0fc2018-09-06 16:24:41 +0300122#define GUARD_MODULE_INITIALIZED \
123 if( global_data.initialized == 0 ) \
124 return( PSA_ERROR_BAD_STATE );
125
Gilles Peskinee59236f2018-01-27 23:32:46 +0100126static psa_status_t mbedtls_to_psa_error( int ret )
127{
Gilles Peskinea5905292018-02-07 20:59:33 +0100128 /* If there's both a high-level code and low-level code, dispatch on
129 * the high-level code. */
130 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100131 {
132 case 0:
133 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100134
135 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
136 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
137 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
138 return( PSA_ERROR_NOT_SUPPORTED );
139 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
140 return( PSA_ERROR_HARDWARE_FAILURE );
141
142 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
143 return( PSA_ERROR_HARDWARE_FAILURE );
144
Gilles Peskine9a944802018-06-21 09:35:35 +0200145 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
146 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
147 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
148 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
149 case MBEDTLS_ERR_ASN1_INVALID_DATA:
150 return( PSA_ERROR_INVALID_ARGUMENT );
151 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
152 return( PSA_ERROR_INSUFFICIENT_MEMORY );
153 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
154 return( PSA_ERROR_BUFFER_TOO_SMALL );
155
Jaeden Amero93e21112019-02-20 13:57:28 +0000156#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000157 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000158#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
159 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
160#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100161 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
162 return( PSA_ERROR_NOT_SUPPORTED );
163 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
164 return( PSA_ERROR_HARDWARE_FAILURE );
165
Jaeden Amero93e21112019-02-20 13:57:28 +0000166#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000167 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000168#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
169 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
170#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100171 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
172 return( PSA_ERROR_NOT_SUPPORTED );
173 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
174 return( PSA_ERROR_HARDWARE_FAILURE );
175
176 case MBEDTLS_ERR_CCM_BAD_INPUT:
177 return( PSA_ERROR_INVALID_ARGUMENT );
178 case MBEDTLS_ERR_CCM_AUTH_FAILED:
179 return( PSA_ERROR_INVALID_SIGNATURE );
180 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
181 return( PSA_ERROR_HARDWARE_FAILURE );
182
183 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
184 return( PSA_ERROR_NOT_SUPPORTED );
185 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
186 return( PSA_ERROR_INVALID_ARGUMENT );
187 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
188 return( PSA_ERROR_INSUFFICIENT_MEMORY );
189 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
190 return( PSA_ERROR_INVALID_PADDING );
191 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
192 return( PSA_ERROR_BAD_STATE );
193 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
194 return( PSA_ERROR_INVALID_SIGNATURE );
195 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
196 return( PSA_ERROR_TAMPERING_DETECTED );
197 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
198 return( PSA_ERROR_HARDWARE_FAILURE );
199
200 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
201 return( PSA_ERROR_HARDWARE_FAILURE );
202
203 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
204 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
205 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
206 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
207 return( PSA_ERROR_NOT_SUPPORTED );
208 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
209 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
210
211 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
212 return( PSA_ERROR_NOT_SUPPORTED );
213 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
214 return( PSA_ERROR_HARDWARE_FAILURE );
215
Gilles Peskinee59236f2018-01-27 23:32:46 +0100216 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
217 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
218 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
219 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100220
221 case MBEDTLS_ERR_GCM_AUTH_FAILED:
222 return( PSA_ERROR_INVALID_SIGNATURE );
223 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200224 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100225 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
226 return( PSA_ERROR_HARDWARE_FAILURE );
227
228 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
229 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
230 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
231 return( PSA_ERROR_HARDWARE_FAILURE );
232
233 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
234 return( PSA_ERROR_NOT_SUPPORTED );
235 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
236 return( PSA_ERROR_INVALID_ARGUMENT );
237 case MBEDTLS_ERR_MD_ALLOC_FAILED:
238 return( PSA_ERROR_INSUFFICIENT_MEMORY );
239 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
240 return( PSA_ERROR_STORAGE_FAILURE );
241 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
242 return( PSA_ERROR_HARDWARE_FAILURE );
243
Gilles Peskinef76aa772018-10-29 19:24:33 +0100244 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
245 return( PSA_ERROR_STORAGE_FAILURE );
246 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
247 return( PSA_ERROR_INVALID_ARGUMENT );
248 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
249 return( PSA_ERROR_INVALID_ARGUMENT );
250 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
251 return( PSA_ERROR_BUFFER_TOO_SMALL );
252 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
253 return( PSA_ERROR_INVALID_ARGUMENT );
254 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
255 return( PSA_ERROR_INVALID_ARGUMENT );
256 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
257 return( PSA_ERROR_INVALID_ARGUMENT );
258 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
259 return( PSA_ERROR_INSUFFICIENT_MEMORY );
260
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100261 case MBEDTLS_ERR_PK_ALLOC_FAILED:
262 return( PSA_ERROR_INSUFFICIENT_MEMORY );
263 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
264 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
265 return( PSA_ERROR_INVALID_ARGUMENT );
266 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100267 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100268 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
269 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
270 return( PSA_ERROR_INVALID_ARGUMENT );
271 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
272 return( PSA_ERROR_NOT_SUPPORTED );
273 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
274 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
275 return( PSA_ERROR_NOT_PERMITTED );
276 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
277 return( PSA_ERROR_INVALID_ARGUMENT );
278 case MBEDTLS_ERR_PK_INVALID_ALG:
279 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
280 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
281 return( PSA_ERROR_NOT_SUPPORTED );
282 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
283 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100284 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
285 return( PSA_ERROR_HARDWARE_FAILURE );
286
287 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
288 return( PSA_ERROR_HARDWARE_FAILURE );
289
290 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
291 return( PSA_ERROR_INVALID_ARGUMENT );
292 case MBEDTLS_ERR_RSA_INVALID_PADDING:
293 return( PSA_ERROR_INVALID_PADDING );
294 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
295 return( PSA_ERROR_HARDWARE_FAILURE );
296 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
297 return( PSA_ERROR_INVALID_ARGUMENT );
298 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
299 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
300 return( PSA_ERROR_TAMPERING_DETECTED );
301 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
302 return( PSA_ERROR_INVALID_SIGNATURE );
303 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
304 return( PSA_ERROR_BUFFER_TOO_SMALL );
305 case MBEDTLS_ERR_RSA_RNG_FAILED:
306 return( PSA_ERROR_INSUFFICIENT_MEMORY );
307 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
308 return( PSA_ERROR_NOT_SUPPORTED );
309 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
310 return( PSA_ERROR_HARDWARE_FAILURE );
311
312 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
313 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
314 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
315 return( PSA_ERROR_HARDWARE_FAILURE );
316
317 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
318 return( PSA_ERROR_INVALID_ARGUMENT );
319 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
320 return( PSA_ERROR_HARDWARE_FAILURE );
321
itayzafrir5c753392018-05-08 11:18:38 +0300322 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300323 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300324 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300325 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
326 return( PSA_ERROR_BUFFER_TOO_SMALL );
327 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
328 return( PSA_ERROR_NOT_SUPPORTED );
329 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
330 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
331 return( PSA_ERROR_INVALID_SIGNATURE );
332 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
333 return( PSA_ERROR_INSUFFICIENT_MEMORY );
334 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
335 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300336
Gilles Peskinee59236f2018-01-27 23:32:46 +0100337 default:
David Saadab4ecc272019-02-14 13:48:10 +0200338 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100339 }
340}
341
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200342
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200343
344
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100345/****************************************************************/
346/* Key management */
347/****************************************************************/
348
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100349#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200350static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
351{
352 switch( grpid )
353 {
354 case MBEDTLS_ECP_DP_SECP192R1:
355 return( PSA_ECC_CURVE_SECP192R1 );
356 case MBEDTLS_ECP_DP_SECP224R1:
357 return( PSA_ECC_CURVE_SECP224R1 );
358 case MBEDTLS_ECP_DP_SECP256R1:
359 return( PSA_ECC_CURVE_SECP256R1 );
360 case MBEDTLS_ECP_DP_SECP384R1:
361 return( PSA_ECC_CURVE_SECP384R1 );
362 case MBEDTLS_ECP_DP_SECP521R1:
363 return( PSA_ECC_CURVE_SECP521R1 );
364 case MBEDTLS_ECP_DP_BP256R1:
365 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
366 case MBEDTLS_ECP_DP_BP384R1:
367 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
368 case MBEDTLS_ECP_DP_BP512R1:
369 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
370 case MBEDTLS_ECP_DP_CURVE25519:
371 return( PSA_ECC_CURVE_CURVE25519 );
372 case MBEDTLS_ECP_DP_SECP192K1:
373 return( PSA_ECC_CURVE_SECP192K1 );
374 case MBEDTLS_ECP_DP_SECP224K1:
375 return( PSA_ECC_CURVE_SECP224K1 );
376 case MBEDTLS_ECP_DP_SECP256K1:
377 return( PSA_ECC_CURVE_SECP256K1 );
378 case MBEDTLS_ECP_DP_CURVE448:
379 return( PSA_ECC_CURVE_CURVE448 );
380 default:
381 return( 0 );
382 }
383}
384
Gilles Peskine12313cd2018-06-20 00:20:32 +0200385static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
386{
387 switch( curve )
388 {
389 case PSA_ECC_CURVE_SECP192R1:
390 return( MBEDTLS_ECP_DP_SECP192R1 );
391 case PSA_ECC_CURVE_SECP224R1:
392 return( MBEDTLS_ECP_DP_SECP224R1 );
393 case PSA_ECC_CURVE_SECP256R1:
394 return( MBEDTLS_ECP_DP_SECP256R1 );
395 case PSA_ECC_CURVE_SECP384R1:
396 return( MBEDTLS_ECP_DP_SECP384R1 );
397 case PSA_ECC_CURVE_SECP521R1:
398 return( MBEDTLS_ECP_DP_SECP521R1 );
399 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
400 return( MBEDTLS_ECP_DP_BP256R1 );
401 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
402 return( MBEDTLS_ECP_DP_BP384R1 );
403 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
404 return( MBEDTLS_ECP_DP_BP512R1 );
405 case PSA_ECC_CURVE_CURVE25519:
406 return( MBEDTLS_ECP_DP_CURVE25519 );
407 case PSA_ECC_CURVE_SECP192K1:
408 return( MBEDTLS_ECP_DP_SECP192K1 );
409 case PSA_ECC_CURVE_SECP224K1:
410 return( MBEDTLS_ECP_DP_SECP224K1 );
411 case PSA_ECC_CURVE_SECP256K1:
412 return( MBEDTLS_ECP_DP_SECP256K1 );
413 case PSA_ECC_CURVE_CURVE448:
414 return( MBEDTLS_ECP_DP_CURVE448 );
415 default:
416 return( MBEDTLS_ECP_DP_NONE );
417 }
418}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100419#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200420
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200421static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
422 size_t bits,
423 struct raw_data *raw )
424{
425 /* Check that the bit size is acceptable for the key type */
426 switch( type )
427 {
428 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200429 if( bits == 0 )
430 {
431 raw->bytes = 0;
432 raw->data = NULL;
433 return( PSA_SUCCESS );
434 }
435 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200436#if defined(MBEDTLS_MD_C)
437 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200438#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200439 case PSA_KEY_TYPE_DERIVE:
440 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200441#if defined(MBEDTLS_AES_C)
442 case PSA_KEY_TYPE_AES:
443 if( bits != 128 && bits != 192 && bits != 256 )
444 return( PSA_ERROR_INVALID_ARGUMENT );
445 break;
446#endif
447#if defined(MBEDTLS_CAMELLIA_C)
448 case PSA_KEY_TYPE_CAMELLIA:
449 if( bits != 128 && bits != 192 && bits != 256 )
450 return( PSA_ERROR_INVALID_ARGUMENT );
451 break;
452#endif
453#if defined(MBEDTLS_DES_C)
454 case PSA_KEY_TYPE_DES:
455 if( bits != 64 && bits != 128 && bits != 192 )
456 return( PSA_ERROR_INVALID_ARGUMENT );
457 break;
458#endif
459#if defined(MBEDTLS_ARC4_C)
460 case PSA_KEY_TYPE_ARC4:
461 if( bits < 8 || bits > 2048 )
462 return( PSA_ERROR_INVALID_ARGUMENT );
463 break;
464#endif
465 default:
466 return( PSA_ERROR_NOT_SUPPORTED );
467 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200468 if( bits % 8 != 0 )
469 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200470
471 /* Allocate memory for the key */
472 raw->bytes = PSA_BITS_TO_BYTES( bits );
473 raw->data = mbedtls_calloc( 1, raw->bytes );
474 if( raw->data == NULL )
475 {
476 raw->bytes = 0;
477 return( PSA_ERROR_INSUFFICIENT_MEMORY );
478 }
479 return( PSA_SUCCESS );
480}
481
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200482#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100483/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
484 * that are not a multiple of 8) well. For example, there is only
485 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
486 * way to return the exact bit size of a key.
487 * To keep things simple, reject non-byte-aligned key sizes. */
488static psa_status_t psa_check_rsa_key_byte_aligned(
489 const mbedtls_rsa_context *rsa )
490{
491 mbedtls_mpi n;
492 psa_status_t status;
493 mbedtls_mpi_init( &n );
494 status = mbedtls_to_psa_error(
495 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
496 if( status == PSA_SUCCESS )
497 {
498 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
499 status = PSA_ERROR_NOT_SUPPORTED;
500 }
501 mbedtls_mpi_free( &n );
502 return( status );
503}
504
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000505static psa_status_t psa_import_rsa_key( psa_key_type_t type,
506 const uint8_t *data,
507 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200508 mbedtls_rsa_context **p_rsa )
509{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000510 psa_status_t status;
511 mbedtls_pk_context pk;
512 mbedtls_rsa_context *rsa;
513 size_t bits;
514
515 mbedtls_pk_init( &pk );
516
517 /* Parse the data. */
518 if( PSA_KEY_TYPE_IS_KEYPAIR( type ) )
519 status = mbedtls_to_psa_error(
520 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200521 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000522 status = mbedtls_to_psa_error(
523 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
524 if( status != PSA_SUCCESS )
525 goto exit;
526
527 /* We have something that the pkparse module recognizes. If it is a
528 * valid RSA key, store it. */
529 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200530 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000531 status = PSA_ERROR_INVALID_ARGUMENT;
532 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200533 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000534
535 rsa = mbedtls_pk_rsa( pk );
536 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
537 * supports non-byte-aligned key sizes, but not well. For example,
538 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
539 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
540 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
541 {
542 status = PSA_ERROR_NOT_SUPPORTED;
543 goto exit;
544 }
545 status = psa_check_rsa_key_byte_aligned( rsa );
546
547exit:
548 /* Free the content of the pk object only on error. */
549 if( status != PSA_SUCCESS )
550 {
551 mbedtls_pk_free( &pk );
552 return( status );
553 }
554
555 /* On success, store the content of the object in the RSA context. */
556 *p_rsa = rsa;
557
558 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200559}
560#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
561
Jaeden Ameroccdce902019-01-10 11:42:27 +0000562#if defined(MBEDTLS_ECP_C)
563
564/* Import a public key given as the uncompressed representation defined by SEC1
565 * 2.3.3 as the content of an ECPoint. */
566static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
567 const uint8_t *data,
568 size_t data_length,
569 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200570{
Jaeden Ameroccdce902019-01-10 11:42:27 +0000571 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
572 mbedtls_ecp_keypair *ecp = NULL;
573 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
574
575 *p_ecp = NULL;
576 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
577 if( ecp == NULL )
578 return( PSA_ERROR_INSUFFICIENT_MEMORY );
579 mbedtls_ecp_keypair_init( ecp );
580
581 /* Load the group. */
582 status = mbedtls_to_psa_error(
583 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
584 if( status != PSA_SUCCESS )
585 goto exit;
586 /* Load the public value. */
587 status = mbedtls_to_psa_error(
588 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
589 data, data_length ) );
590 if( status != PSA_SUCCESS )
591 goto exit;
592
593 /* Check that the point is on the curve. */
594 status = mbedtls_to_psa_error(
595 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
596 if( status != PSA_SUCCESS )
597 goto exit;
598
599 *p_ecp = ecp;
600 return( PSA_SUCCESS );
601
602exit:
603 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200604 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000605 mbedtls_ecp_keypair_free( ecp );
606 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200607 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000608 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200609}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000610#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200611
Gilles Peskinef76aa772018-10-29 19:24:33 +0100612#if defined(MBEDTLS_ECP_C)
613/* Import a private key given as a byte string which is the private value
614 * in big-endian order. */
615static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
616 const uint8_t *data,
617 size_t data_length,
618 mbedtls_ecp_keypair **p_ecp )
619{
620 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
621 mbedtls_ecp_keypair *ecp = NULL;
622 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
623
624 *p_ecp = NULL;
625 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
626 if( ecp == NULL )
627 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000628 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100629
630 /* Load the group. */
631 status = mbedtls_to_psa_error(
632 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
633 if( status != PSA_SUCCESS )
634 goto exit;
635 /* Load the secret value. */
636 status = mbedtls_to_psa_error(
637 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
638 if( status != PSA_SUCCESS )
639 goto exit;
640 /* Validate the private key. */
641 status = mbedtls_to_psa_error(
642 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
643 if( status != PSA_SUCCESS )
644 goto exit;
645 /* Calculate the public key from the private key. */
646 status = mbedtls_to_psa_error(
647 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
648 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
649 if( status != PSA_SUCCESS )
650 goto exit;
651
652 *p_ecp = ecp;
653 return( PSA_SUCCESS );
654
655exit:
656 if( ecp != NULL )
657 {
658 mbedtls_ecp_keypair_free( ecp );
659 mbedtls_free( ecp );
660 }
661 return( status );
662}
663#endif /* defined(MBEDTLS_ECP_C) */
664
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100665/** Import key data into a slot. `slot->type` must have been set
666 * previously. This function assumes that the slot does not contain
667 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100668psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
669 const uint8_t *data,
670 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100671{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200672 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100673
Darryl Green940d72c2018-07-13 13:18:51 +0100674 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100675 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100676 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100677 if( data_length > SIZE_MAX / 8 )
678 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green940d72c2018-07-13 13:18:51 +0100679 status = prepare_raw_data_slot( slot->type,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200680 PSA_BYTES_TO_BITS( data_length ),
681 &slot->data.raw );
682 if( status != PSA_SUCCESS )
683 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200684 if( data_length != 0 )
685 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100686 }
687 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100688#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100689 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100690 {
Darryl Green940d72c2018-07-13 13:18:51 +0100691 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100692 data, data_length,
693 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100694 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000695 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100696 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000697 status = psa_import_ec_public_key(
698 PSA_KEY_TYPE_GET_CURVE( slot->type ),
699 data, data_length,
700 &slot->data.ecp );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100701 }
702 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100703#endif /* MBEDTLS_ECP_C */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000704#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
705 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100706 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000707 status = psa_import_rsa_key( slot->type,
708 data, data_length,
709 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100710 }
711 else
Jaeden Ameroccdce902019-01-10 11:42:27 +0000712#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100713 {
714 return( PSA_ERROR_NOT_SUPPORTED );
715 }
Jaeden Ameroc67200d2019-01-14 13:12:39 +0000716 return( status );
Darryl Green940d72c2018-07-13 13:18:51 +0100717}
718
Darryl Green06fd18d2018-07-16 11:21:11 +0100719/* Retrieve an empty key slot (slot with no key data, but possibly
720 * with some metadata such as a policy). */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100721static psa_status_t psa_get_empty_key_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100722 psa_key_slot_t **p_slot )
Darryl Green06fd18d2018-07-16 11:21:11 +0100723{
724 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100725 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100726
727 *p_slot = NULL;
728
Gilles Peskinec5487a82018-12-03 18:08:14 +0100729 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100730 if( status != PSA_SUCCESS )
731 return( status );
732
733 if( slot->type != PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200734 return( PSA_ERROR_ALREADY_EXISTS );
Darryl Green06fd18d2018-07-16 11:21:11 +0100735
736 *p_slot = slot;
737 return( status );
738}
739
Gilles Peskinef603c712019-01-19 13:40:11 +0100740/** Calculate the intersection of two algorithm usage policies.
741 *
742 * Return 0 (which allows no operation) on incompatibility.
743 */
744static psa_algorithm_t psa_key_policy_algorithm_intersection(
745 psa_algorithm_t alg1,
746 psa_algorithm_t alg2 )
747{
748 /* Common case: the policy only allows alg. */
749 if( alg1 == alg2 )
750 return( alg1 );
751 /* If the policies are from the same hash-and-sign family, check
752 * if one is a wildcard. If so the other has the specific algorithm. */
753 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
754 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
755 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
756 {
757 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
758 return( alg2 );
759 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
760 return( alg1 );
761 }
762 /* If the policies are incompatible, allow nothing. */
763 return( 0 );
764}
765
Gilles Peskined6f371b2019-05-10 19:33:38 +0200766static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
767 psa_algorithm_t requested_alg )
768{
769 /* Common case: the policy only allows alg. */
770 if( requested_alg == policy_alg )
771 return( 1 );
772 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
773 * and alg is the same hash-and-sign family with any hash,
774 * then alg is compliant with policy_alg. */
775 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
776 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
777 {
778 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
779 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
780 }
781 /* If it isn't permitted, it's forbidden. */
782 return( 0 );
783}
784
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100785/** Test whether a policy permits an algorithm.
786 *
787 * The caller must test usage flags separately.
788 */
789static int psa_key_policy_permits( const psa_key_policy_t *policy,
790 psa_algorithm_t alg )
791{
Gilles Peskined6f371b2019-05-10 19:33:38 +0200792 return( psa_key_algorithm_permits( policy->alg, alg ) ||
793 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100794}
795
Gilles Peskinef603c712019-01-19 13:40:11 +0100796/** Restrict a key policy based on a constraint.
797 *
798 * \param[in,out] policy The policy to restrict.
799 * \param[in] constraint The policy constraint to apply.
800 *
801 * \retval #PSA_SUCCESS
802 * \c *policy contains the intersection of the original value of
803 * \c *policy and \c *constraint.
804 * \retval #PSA_ERROR_INVALID_ARGUMENT
805 * \c *policy and \c *constraint are incompatible.
806 * \c *policy is unchanged.
807 */
808static psa_status_t psa_restrict_key_policy(
809 psa_key_policy_t *policy,
810 const psa_key_policy_t *constraint )
811{
812 psa_algorithm_t intersection_alg =
813 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200814 psa_algorithm_t intersection_alg2 =
815 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100816 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
817 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200818 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
819 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100820 policy->usage &= constraint->usage;
821 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200822 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100823 return( PSA_SUCCESS );
824}
825
Darryl Green06fd18d2018-07-16 11:21:11 +0100826/** Retrieve a slot which must contain a key. The key must have allow all the
827 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
828 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100829static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100830 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100831 psa_key_usage_t usage,
832 psa_algorithm_t alg )
833{
834 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100835 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100836
837 *p_slot = NULL;
838
Gilles Peskinec5487a82018-12-03 18:08:14 +0100839 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100840 if( status != PSA_SUCCESS )
841 return( status );
842 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200843 return( PSA_ERROR_DOES_NOT_EXIST );
Darryl Green06fd18d2018-07-16 11:21:11 +0100844
845 /* Enforce that usage policy for the key slot contains all the flags
846 * required by the usage parameter. There is one exception: public
847 * keys can always be exported, so we treat public key objects as
848 * if they had the export flag. */
849 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
850 usage &= ~PSA_KEY_USAGE_EXPORT;
851 if( ( slot->policy.usage & usage ) != usage )
852 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100853
854 /* Enforce that the usage policy permits the requested algortihm. */
855 if( alg != 0 && ! psa_key_policy_permits( &slot->policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100856 return( PSA_ERROR_NOT_PERMITTED );
857
858 *p_slot = slot;
859 return( PSA_SUCCESS );
860}
Darryl Green940d72c2018-07-13 13:18:51 +0100861
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100862/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100863static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000864{
865 if( slot->type == PSA_KEY_TYPE_NONE )
866 {
867 /* No key material to clean. */
868 }
869 else if( key_type_is_raw_bytes( slot->type ) )
870 {
871 mbedtls_free( slot->data.raw.data );
872 }
873 else
874#if defined(MBEDTLS_RSA_C)
875 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
876 {
877 mbedtls_rsa_free( slot->data.rsa );
878 mbedtls_free( slot->data.rsa );
879 }
880 else
881#endif /* defined(MBEDTLS_RSA_C) */
882#if defined(MBEDTLS_ECP_C)
883 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
884 {
885 mbedtls_ecp_keypair_free( slot->data.ecp );
886 mbedtls_free( slot->data.ecp );
887 }
888 else
889#endif /* defined(MBEDTLS_ECP_C) */
890 {
891 /* Shouldn't happen: the key type is not any type that we
892 * put in. */
893 return( PSA_ERROR_TAMPERING_DETECTED );
894 }
895
896 return( PSA_SUCCESS );
897}
898
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100899/** Completely wipe a slot in memory, including its policy.
900 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100901psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100902{
903 psa_status_t status = psa_remove_key_data_from_memory( slot );
904 /* At this point, key material and other type-specific content has
905 * been wiped. Clear remaining metadata. We can call memset and not
906 * zeroize because the metadata is not particularly sensitive. */
907 memset( slot, 0, sizeof( *slot ) );
908 return( status );
909}
910
Gilles Peskinec5487a82018-12-03 18:08:14 +0100911psa_status_t psa_import_key( psa_key_handle_t handle,
Darryl Green940d72c2018-07-13 13:18:51 +0100912 psa_key_type_t type,
913 const uint8_t *data,
914 size_t data_length )
915{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100916 psa_key_slot_t *slot;
Darryl Green940d72c2018-07-13 13:18:51 +0100917 psa_status_t status;
918
Gilles Peskinec5487a82018-12-03 18:08:14 +0100919 status = psa_get_empty_key_slot( handle, &slot );
Darryl Green940d72c2018-07-13 13:18:51 +0100920 if( status != PSA_SUCCESS )
921 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100922
923 slot->type = type;
Darryl Green940d72c2018-07-13 13:18:51 +0100924
925 status = psa_import_key_into_slot( slot, data, data_length );
926 if( status != PSA_SUCCESS )
927 {
928 slot->type = PSA_KEY_TYPE_NONE;
929 return( status );
930 }
931
Darryl Greend49a4992018-06-18 17:27:26 +0100932#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
933 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
934 {
935 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +0100936 status = psa_save_persistent_key( slot->persistent_storage_id,
937 slot->type, &slot->policy, data,
Darryl Greend49a4992018-06-18 17:27:26 +0100938 data_length );
939 if( status != PSA_SUCCESS )
940 {
941 (void) psa_remove_key_data_from_memory( slot );
942 slot->type = PSA_KEY_TYPE_NONE;
943 }
944 }
945#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
946
947 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100948}
949
Gilles Peskinec5487a82018-12-03 18:08:14 +0100950psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100951{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100952 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100953 psa_status_t status = PSA_SUCCESS;
954 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100955
Gilles Peskinec5487a82018-12-03 18:08:14 +0100956 status = psa_get_key_slot( handle, &slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100957 if( status != PSA_SUCCESS )
958 return( status );
Darryl Greend49a4992018-06-18 17:27:26 +0100959#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
960 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
961 {
Gilles Peskine69f976b2018-11-30 18:46:56 +0100962 storage_status =
963 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +0100964 }
965#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100966 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +0100967 if( status != PSA_SUCCESS )
968 return( status );
969 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100970}
971
Gilles Peskineb870b182018-07-06 16:02:09 +0200972/* Return the size of the key in the given slot, in bits. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100973static size_t psa_get_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +0200974{
975 if( key_type_is_raw_bytes( slot->type ) )
976 return( slot->data.raw.bytes * 8 );
977#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200978 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +0100979 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +0200980#endif /* defined(MBEDTLS_RSA_C) */
981#if defined(MBEDTLS_ECP_C)
982 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
983 return( slot->data.ecp->grp.pbits );
984#endif /* defined(MBEDTLS_ECP_C) */
985 /* Shouldn't happen except on an empty slot. */
986 return( 0 );
987}
988
Gilles Peskinec5487a82018-12-03 18:08:14 +0100989psa_status_t psa_get_key_information( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +0200990 psa_key_type_t *type,
991 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100992{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100993 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200994 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100995
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100996 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200997 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100998 if( bits != NULL )
999 *bits = 0;
Gilles Peskinec5487a82018-12-03 18:08:14 +01001000 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001001 if( status != PSA_SUCCESS )
1002 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001003
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001004 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +02001005 return( PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001006 if( type != NULL )
1007 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +02001008 if( bits != NULL )
1009 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001010 return( PSA_SUCCESS );
1011}
1012
Jaeden Ameroccdce902019-01-10 11:42:27 +00001013#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero25384a22019-01-10 10:23:21 +00001014static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1015 unsigned char *buf, size_t size )
1016{
1017 int ret;
1018 unsigned char *c;
1019 size_t len = 0;
1020
1021 c = buf + size;
1022
1023 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1024
1025 return( (int) len );
1026}
Jaeden Ameroccdce902019-01-10 11:42:27 +00001027#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero25384a22019-01-10 10:23:21 +00001028
Gilles Peskinef603c712019-01-19 13:40:11 +01001029static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1030 uint8_t *data,
1031 size_t data_size,
1032 size_t *data_length,
1033 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001034{
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001035 *data_length = 0;
1036
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001037 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001038 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001039
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001040 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001041 {
1042 if( slot->data.raw.bytes > data_size )
1043 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001044 if( data_size != 0 )
1045 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001046 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001047 memset( data + slot->data.raw.bytes, 0,
1048 data_size - slot->data.raw.bytes );
1049 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001050 *data_length = slot->data.raw.bytes;
1051 return( PSA_SUCCESS );
1052 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001053#if defined(MBEDTLS_ECP_C)
1054 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
1055 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001056 psa_status_t status;
1057
Gilles Peskine188c71e2018-10-29 19:26:02 +01001058 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( slot ) );
1059 if( bytes > data_size )
1060 return( PSA_ERROR_BUFFER_TOO_SMALL );
1061 status = mbedtls_to_psa_error(
1062 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1063 if( status != PSA_SUCCESS )
1064 return( status );
1065 memset( data + bytes, 0, data_size - bytes );
1066 *data_length = bytes;
1067 return( PSA_SUCCESS );
1068 }
1069#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001070 else
Moran Peker17e36e12018-05-02 12:55:20 +03001071 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001072#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001073 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001074 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001075 {
Moran Pekera998bc62018-04-16 18:16:20 +03001076 mbedtls_pk_context pk;
1077 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001078 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001079 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001080#if defined(MBEDTLS_RSA_C)
1081 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001082 pk.pk_info = &mbedtls_rsa_info;
1083 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001084#else
1085 return( PSA_ERROR_NOT_SUPPORTED );
1086#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001087 }
1088 else
1089 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001090#if defined(MBEDTLS_ECP_C)
1091 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001092 pk.pk_info = &mbedtls_eckey_info;
1093 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001094#else
1095 return( PSA_ERROR_NOT_SUPPORTED );
1096#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001097 }
Moran Pekerd7326592018-05-29 16:56:39 +03001098 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Jaeden Amero25384a22019-01-10 10:23:21 +00001099 {
Jaeden Ameroccdce902019-01-10 11:42:27 +00001100 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001101 }
Moran Peker17e36e12018-05-02 12:55:20 +03001102 else
Jaeden Amero25384a22019-01-10 10:23:21 +00001103 {
Moran Peker17e36e12018-05-02 12:55:20 +03001104 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001105 }
Moran Peker60364322018-04-29 11:34:58 +03001106 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001107 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001108 /* If data_size is 0 then data may be NULL and then the
1109 * call to memset would have undefined behavior. */
1110 if( data_size != 0 )
1111 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001112 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001113 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001114 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1115 * Move the data to the beginning and erase remaining data
1116 * at the original location. */
1117 if( 2 * (size_t) ret <= data_size )
1118 {
1119 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001120 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001121 }
1122 else if( (size_t) ret < data_size )
1123 {
1124 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001125 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001126 }
Moran Pekera998bc62018-04-16 18:16:20 +03001127 *data_length = ret;
1128 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001129 }
1130 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001131#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001132 {
1133 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001134 it is valid for a special-purpose implementation to omit
1135 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001136 return( PSA_ERROR_NOT_SUPPORTED );
1137 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001138 }
1139}
1140
Gilles Peskinec5487a82018-12-03 18:08:14 +01001141psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001142 uint8_t *data,
1143 size_t data_size,
1144 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001145{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001146 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001147 psa_status_t status;
1148
1149 /* Set the key to empty now, so that even when there are errors, we always
1150 * set data_length to a value between 0 and data_size. On error, setting
1151 * the key to empty is a good choice because an empty key representation is
1152 * unlikely to be accepted anywhere. */
1153 *data_length = 0;
1154
1155 /* Export requires the EXPORT flag. There is an exception for public keys,
1156 * which don't require any flag, but psa_get_key_from_slot takes
1157 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001158 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001159 if( status != PSA_SUCCESS )
1160 return( status );
1161 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001162 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001163}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001164
Gilles Peskinec5487a82018-12-03 18:08:14 +01001165psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001166 uint8_t *data,
1167 size_t data_size,
1168 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001169{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001170 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001171 psa_status_t status;
1172
1173 /* Set the key to empty now, so that even when there are errors, we always
1174 * set data_length to a value between 0 and data_size. On error, setting
1175 * the key to empty is a good choice because an empty key representation is
1176 * unlikely to be accepted anywhere. */
1177 *data_length = 0;
1178
1179 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001180 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001181 if( status != PSA_SUCCESS )
1182 return( status );
1183 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001184 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001185}
1186
Darryl Green0c6575a2018-11-07 16:05:30 +00001187#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine2f060a82018-12-04 17:12:32 +01001188static psa_status_t psa_save_generated_persistent_key( psa_key_slot_t *slot,
Darryl Green0c6575a2018-11-07 16:05:30 +00001189 size_t bits )
1190{
1191 psa_status_t status;
1192 uint8_t *data;
1193 size_t key_length;
1194 size_t data_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type, bits );
1195 data = mbedtls_calloc( 1, data_size );
itayzafrir910c76b2018-11-21 16:03:21 +02001196 if( data == NULL )
1197 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Darryl Green0c6575a2018-11-07 16:05:30 +00001198 /* Get key data in export format */
1199 status = psa_internal_export_key( slot, data, data_size, &key_length, 0 );
1200 if( status != PSA_SUCCESS )
1201 {
1202 slot->type = PSA_KEY_TYPE_NONE;
1203 goto exit;
1204 }
1205 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +01001206 status = psa_save_persistent_key( slot->persistent_storage_id,
1207 slot->type, &slot->policy,
Darryl Green0c6575a2018-11-07 16:05:30 +00001208 data, key_length );
1209 if( status != PSA_SUCCESS )
1210 {
1211 slot->type = PSA_KEY_TYPE_NONE;
1212 }
1213exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01001214 mbedtls_platform_zeroize( data, key_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00001215 mbedtls_free( data );
1216 return( status );
1217}
1218#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1219
Gilles Peskinef603c712019-01-19 13:40:11 +01001220static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
1221 psa_key_handle_t target )
1222{
1223 psa_status_t status;
1224 uint8_t *buffer = NULL;
1225 size_t buffer_size = 0;
1226 size_t length;
1227
1228 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->type,
1229 psa_get_key_bits( source ) );
1230 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001231 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001232 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01001233 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1234 if( status != PSA_SUCCESS )
1235 goto exit;
1236 status = psa_import_key( target, source->type, buffer, length );
1237
1238exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001239 if( buffer_size != 0 )
1240 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001241 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01001242 return( status );
1243}
1244
1245psa_status_t psa_copy_key(psa_key_handle_t source_handle,
1246 psa_key_handle_t target_handle,
1247 const psa_key_policy_t *constraint)
1248{
1249 psa_key_slot_t *source_slot = NULL;
1250 psa_key_slot_t *target_slot = NULL;
1251 psa_key_policy_t new_policy;
1252 psa_status_t status;
1253 status = psa_get_key_from_slot( source_handle, &source_slot, 0, 0 );
1254 if( status != PSA_SUCCESS )
1255 return( status );
1256 status = psa_get_empty_key_slot( target_handle, &target_slot );
1257 if( status != PSA_SUCCESS )
1258 return( status );
1259
1260 new_policy = target_slot->policy;
1261 status = psa_restrict_key_policy( &new_policy, &source_slot->policy );
1262 if( status != PSA_SUCCESS )
1263 return( status );
1264 if( constraint != NULL )
1265 {
1266 status = psa_restrict_key_policy( &new_policy, constraint );
1267 if( status != PSA_SUCCESS )
1268 return( status );
1269 }
1270
1271 status = psa_copy_key_material( source_slot, target_handle );
1272 if( status != PSA_SUCCESS )
1273 return( status );
1274
1275 target_slot->policy = new_policy;
1276 return( PSA_SUCCESS );
1277}
1278
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001279
1280
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001281/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001282/* Message digests */
1283/****************************************************************/
1284
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001285static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001286{
1287 switch( alg )
1288 {
1289#if defined(MBEDTLS_MD2_C)
1290 case PSA_ALG_MD2:
1291 return( &mbedtls_md2_info );
1292#endif
1293#if defined(MBEDTLS_MD4_C)
1294 case PSA_ALG_MD4:
1295 return( &mbedtls_md4_info );
1296#endif
1297#if defined(MBEDTLS_MD5_C)
1298 case PSA_ALG_MD5:
1299 return( &mbedtls_md5_info );
1300#endif
1301#if defined(MBEDTLS_RIPEMD160_C)
1302 case PSA_ALG_RIPEMD160:
1303 return( &mbedtls_ripemd160_info );
1304#endif
1305#if defined(MBEDTLS_SHA1_C)
1306 case PSA_ALG_SHA_1:
1307 return( &mbedtls_sha1_info );
1308#endif
1309#if defined(MBEDTLS_SHA256_C)
1310 case PSA_ALG_SHA_224:
1311 return( &mbedtls_sha224_info );
1312 case PSA_ALG_SHA_256:
1313 return( &mbedtls_sha256_info );
1314#endif
1315#if defined(MBEDTLS_SHA512_C)
1316 case PSA_ALG_SHA_384:
1317 return( &mbedtls_sha384_info );
1318 case PSA_ALG_SHA_512:
1319 return( &mbedtls_sha512_info );
1320#endif
1321 default:
1322 return( NULL );
1323 }
1324}
1325
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001326psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1327{
1328 switch( operation->alg )
1329 {
Gilles Peskine81736312018-06-26 15:04:31 +02001330 case 0:
1331 /* The object has (apparently) been initialized but it is not
1332 * in use. It's ok to call abort on such an object, and there's
1333 * nothing to do. */
1334 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001335#if defined(MBEDTLS_MD2_C)
1336 case PSA_ALG_MD2:
1337 mbedtls_md2_free( &operation->ctx.md2 );
1338 break;
1339#endif
1340#if defined(MBEDTLS_MD4_C)
1341 case PSA_ALG_MD4:
1342 mbedtls_md4_free( &operation->ctx.md4 );
1343 break;
1344#endif
1345#if defined(MBEDTLS_MD5_C)
1346 case PSA_ALG_MD5:
1347 mbedtls_md5_free( &operation->ctx.md5 );
1348 break;
1349#endif
1350#if defined(MBEDTLS_RIPEMD160_C)
1351 case PSA_ALG_RIPEMD160:
1352 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1353 break;
1354#endif
1355#if defined(MBEDTLS_SHA1_C)
1356 case PSA_ALG_SHA_1:
1357 mbedtls_sha1_free( &operation->ctx.sha1 );
1358 break;
1359#endif
1360#if defined(MBEDTLS_SHA256_C)
1361 case PSA_ALG_SHA_224:
1362 case PSA_ALG_SHA_256:
1363 mbedtls_sha256_free( &operation->ctx.sha256 );
1364 break;
1365#endif
1366#if defined(MBEDTLS_SHA512_C)
1367 case PSA_ALG_SHA_384:
1368 case PSA_ALG_SHA_512:
1369 mbedtls_sha512_free( &operation->ctx.sha512 );
1370 break;
1371#endif
1372 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001373 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001374 }
1375 operation->alg = 0;
1376 return( PSA_SUCCESS );
1377}
1378
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001379psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001380 psa_algorithm_t alg )
1381{
1382 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001383
1384 /* A context must be freshly initialized before it can be set up. */
1385 if( operation->alg != 0 )
1386 {
1387 return( PSA_ERROR_BAD_STATE );
1388 }
1389
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001390 switch( alg )
1391 {
1392#if defined(MBEDTLS_MD2_C)
1393 case PSA_ALG_MD2:
1394 mbedtls_md2_init( &operation->ctx.md2 );
1395 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1396 break;
1397#endif
1398#if defined(MBEDTLS_MD4_C)
1399 case PSA_ALG_MD4:
1400 mbedtls_md4_init( &operation->ctx.md4 );
1401 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1402 break;
1403#endif
1404#if defined(MBEDTLS_MD5_C)
1405 case PSA_ALG_MD5:
1406 mbedtls_md5_init( &operation->ctx.md5 );
1407 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1408 break;
1409#endif
1410#if defined(MBEDTLS_RIPEMD160_C)
1411 case PSA_ALG_RIPEMD160:
1412 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1413 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1414 break;
1415#endif
1416#if defined(MBEDTLS_SHA1_C)
1417 case PSA_ALG_SHA_1:
1418 mbedtls_sha1_init( &operation->ctx.sha1 );
1419 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1420 break;
1421#endif
1422#if defined(MBEDTLS_SHA256_C)
1423 case PSA_ALG_SHA_224:
1424 mbedtls_sha256_init( &operation->ctx.sha256 );
1425 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1426 break;
1427 case PSA_ALG_SHA_256:
1428 mbedtls_sha256_init( &operation->ctx.sha256 );
1429 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1430 break;
1431#endif
1432#if defined(MBEDTLS_SHA512_C)
1433 case PSA_ALG_SHA_384:
1434 mbedtls_sha512_init( &operation->ctx.sha512 );
1435 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1436 break;
1437 case PSA_ALG_SHA_512:
1438 mbedtls_sha512_init( &operation->ctx.sha512 );
1439 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1440 break;
1441#endif
1442 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001443 return( PSA_ALG_IS_HASH( alg ) ?
1444 PSA_ERROR_NOT_SUPPORTED :
1445 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001446 }
1447 if( ret == 0 )
1448 operation->alg = alg;
1449 else
1450 psa_hash_abort( operation );
1451 return( mbedtls_to_psa_error( ret ) );
1452}
1453
1454psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1455 const uint8_t *input,
1456 size_t input_length )
1457{
1458 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001459
1460 /* Don't require hash implementations to behave correctly on a
1461 * zero-length input, which may have an invalid pointer. */
1462 if( input_length == 0 )
1463 return( PSA_SUCCESS );
1464
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001465 switch( operation->alg )
1466 {
1467#if defined(MBEDTLS_MD2_C)
1468 case PSA_ALG_MD2:
1469 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1470 input, input_length );
1471 break;
1472#endif
1473#if defined(MBEDTLS_MD4_C)
1474 case PSA_ALG_MD4:
1475 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1476 input, input_length );
1477 break;
1478#endif
1479#if defined(MBEDTLS_MD5_C)
1480 case PSA_ALG_MD5:
1481 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1482 input, input_length );
1483 break;
1484#endif
1485#if defined(MBEDTLS_RIPEMD160_C)
1486 case PSA_ALG_RIPEMD160:
1487 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1488 input, input_length );
1489 break;
1490#endif
1491#if defined(MBEDTLS_SHA1_C)
1492 case PSA_ALG_SHA_1:
1493 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1494 input, input_length );
1495 break;
1496#endif
1497#if defined(MBEDTLS_SHA256_C)
1498 case PSA_ALG_SHA_224:
1499 case PSA_ALG_SHA_256:
1500 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1501 input, input_length );
1502 break;
1503#endif
1504#if defined(MBEDTLS_SHA512_C)
1505 case PSA_ALG_SHA_384:
1506 case PSA_ALG_SHA_512:
1507 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1508 input, input_length );
1509 break;
1510#endif
1511 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001512 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001513 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001514
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001515 if( ret != 0 )
1516 psa_hash_abort( operation );
1517 return( mbedtls_to_psa_error( ret ) );
1518}
1519
1520psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1521 uint8_t *hash,
1522 size_t hash_size,
1523 size_t *hash_length )
1524{
itayzafrir40835d42018-08-02 13:14:17 +03001525 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001526 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001527 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001528
1529 /* Fill the output buffer with something that isn't a valid hash
1530 * (barring an attack on the hash and deliberately-crafted input),
1531 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001532 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001533 /* If hash_size is 0 then hash may be NULL and then the
1534 * call to memset would have undefined behavior. */
1535 if( hash_size != 0 )
1536 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001537
1538 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001539 {
1540 status = PSA_ERROR_BUFFER_TOO_SMALL;
1541 goto exit;
1542 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001543
1544 switch( operation->alg )
1545 {
1546#if defined(MBEDTLS_MD2_C)
1547 case PSA_ALG_MD2:
1548 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1549 break;
1550#endif
1551#if defined(MBEDTLS_MD4_C)
1552 case PSA_ALG_MD4:
1553 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1554 break;
1555#endif
1556#if defined(MBEDTLS_MD5_C)
1557 case PSA_ALG_MD5:
1558 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1559 break;
1560#endif
1561#if defined(MBEDTLS_RIPEMD160_C)
1562 case PSA_ALG_RIPEMD160:
1563 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1564 break;
1565#endif
1566#if defined(MBEDTLS_SHA1_C)
1567 case PSA_ALG_SHA_1:
1568 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1569 break;
1570#endif
1571#if defined(MBEDTLS_SHA256_C)
1572 case PSA_ALG_SHA_224:
1573 case PSA_ALG_SHA_256:
1574 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1575 break;
1576#endif
1577#if defined(MBEDTLS_SHA512_C)
1578 case PSA_ALG_SHA_384:
1579 case PSA_ALG_SHA_512:
1580 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1581 break;
1582#endif
1583 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001584 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001585 }
itayzafrir40835d42018-08-02 13:14:17 +03001586 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001587
itayzafrir40835d42018-08-02 13:14:17 +03001588exit:
1589 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001590 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001591 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001592 return( psa_hash_abort( operation ) );
1593 }
1594 else
1595 {
1596 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001597 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001598 }
1599}
1600
Gilles Peskine2d277862018-06-18 15:41:12 +02001601psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1602 const uint8_t *hash,
1603 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001604{
1605 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1606 size_t actual_hash_length;
1607 psa_status_t status = psa_hash_finish( operation,
1608 actual_hash, sizeof( actual_hash ),
1609 &actual_hash_length );
1610 if( status != PSA_SUCCESS )
1611 return( status );
1612 if( actual_hash_length != hash_length )
1613 return( PSA_ERROR_INVALID_SIGNATURE );
1614 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1615 return( PSA_ERROR_INVALID_SIGNATURE );
1616 return( PSA_SUCCESS );
1617}
1618
Gilles Peskineeb35d782019-01-22 17:56:16 +01001619psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
1620 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001621{
1622 if( target_operation->alg != 0 )
1623 return( PSA_ERROR_BAD_STATE );
1624
1625 switch( source_operation->alg )
1626 {
1627 case 0:
1628 return( PSA_ERROR_BAD_STATE );
1629#if defined(MBEDTLS_MD2_C)
1630 case PSA_ALG_MD2:
1631 mbedtls_md2_clone( &target_operation->ctx.md2,
1632 &source_operation->ctx.md2 );
1633 break;
1634#endif
1635#if defined(MBEDTLS_MD4_C)
1636 case PSA_ALG_MD4:
1637 mbedtls_md4_clone( &target_operation->ctx.md4,
1638 &source_operation->ctx.md4 );
1639 break;
1640#endif
1641#if defined(MBEDTLS_MD5_C)
1642 case PSA_ALG_MD5:
1643 mbedtls_md5_clone( &target_operation->ctx.md5,
1644 &source_operation->ctx.md5 );
1645 break;
1646#endif
1647#if defined(MBEDTLS_RIPEMD160_C)
1648 case PSA_ALG_RIPEMD160:
1649 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
1650 &source_operation->ctx.ripemd160 );
1651 break;
1652#endif
1653#if defined(MBEDTLS_SHA1_C)
1654 case PSA_ALG_SHA_1:
1655 mbedtls_sha1_clone( &target_operation->ctx.sha1,
1656 &source_operation->ctx.sha1 );
1657 break;
1658#endif
1659#if defined(MBEDTLS_SHA256_C)
1660 case PSA_ALG_SHA_224:
1661 case PSA_ALG_SHA_256:
1662 mbedtls_sha256_clone( &target_operation->ctx.sha256,
1663 &source_operation->ctx.sha256 );
1664 break;
1665#endif
1666#if defined(MBEDTLS_SHA512_C)
1667 case PSA_ALG_SHA_384:
1668 case PSA_ALG_SHA_512:
1669 mbedtls_sha512_clone( &target_operation->ctx.sha512,
1670 &source_operation->ctx.sha512 );
1671 break;
1672#endif
1673 default:
1674 return( PSA_ERROR_NOT_SUPPORTED );
1675 }
1676
1677 target_operation->alg = source_operation->alg;
1678 return( PSA_SUCCESS );
1679}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001680
1681
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001682/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001683/* MAC */
1684/****************************************************************/
1685
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001686static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001687 psa_algorithm_t alg,
1688 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001689 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001690 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001691{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001692 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001693 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001694
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001695 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001696 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001697
Gilles Peskine8c9def32018-02-08 10:02:12 +01001698 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1699 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001700 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001701 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001702 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001703 mode = MBEDTLS_MODE_STREAM;
1704 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001705 case PSA_ALG_CTR:
1706 mode = MBEDTLS_MODE_CTR;
1707 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001708 case PSA_ALG_CFB:
1709 mode = MBEDTLS_MODE_CFB;
1710 break;
1711 case PSA_ALG_OFB:
1712 mode = MBEDTLS_MODE_OFB;
1713 break;
1714 case PSA_ALG_CBC_NO_PADDING:
1715 mode = MBEDTLS_MODE_CBC;
1716 break;
1717 case PSA_ALG_CBC_PKCS7:
1718 mode = MBEDTLS_MODE_CBC;
1719 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001720 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001721 mode = MBEDTLS_MODE_CCM;
1722 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001723 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001724 mode = MBEDTLS_MODE_GCM;
1725 break;
1726 default:
1727 return( NULL );
1728 }
1729 }
1730 else if( alg == PSA_ALG_CMAC )
1731 mode = MBEDTLS_MODE_ECB;
1732 else if( alg == PSA_ALG_GMAC )
1733 mode = MBEDTLS_MODE_GCM;
1734 else
1735 return( NULL );
1736
1737 switch( key_type )
1738 {
1739 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001740 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001741 break;
1742 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001743 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1744 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001745 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001746 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001747 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001748 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001749 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1750 * but two-key Triple-DES is functionally three-key Triple-DES
1751 * with K1=K3, so that's how we present it to mbedtls. */
1752 if( key_bits == 128 )
1753 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001754 break;
1755 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001756 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001757 break;
1758 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001759 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001760 break;
1761 default:
1762 return( NULL );
1763 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001764 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001765 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001766
Jaeden Amero23bbb752018-06-26 14:16:54 +01001767 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1768 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001769}
1770
Gilles Peskinea05219c2018-11-16 16:02:56 +01001771#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001772static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001773{
Gilles Peskine2d277862018-06-18 15:41:12 +02001774 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001775 {
1776 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001777 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001778 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001779 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001780 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001781 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001782 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001783 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001784 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001785 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001786 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001787 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001788 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001789 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001790 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001791 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001792 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001793 return( 128 );
1794 default:
1795 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001796 }
1797}
Gilles Peskinea05219c2018-11-16 16:02:56 +01001798#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001799
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001800/* Initialize the MAC operation structure. Once this function has been
1801 * called, psa_mac_abort can run and will do the right thing. */
1802static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1803 psa_algorithm_t alg )
1804{
1805 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1806
1807 operation->alg = alg;
1808 operation->key_set = 0;
1809 operation->iv_set = 0;
1810 operation->iv_required = 0;
1811 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001812 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001813
1814#if defined(MBEDTLS_CMAC_C)
1815 if( alg == PSA_ALG_CMAC )
1816 {
1817 operation->iv_required = 0;
1818 mbedtls_cipher_init( &operation->ctx.cmac );
1819 status = PSA_SUCCESS;
1820 }
1821 else
1822#endif /* MBEDTLS_CMAC_C */
1823#if defined(MBEDTLS_MD_C)
1824 if( PSA_ALG_IS_HMAC( operation->alg ) )
1825 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001826 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1827 operation->ctx.hmac.hash_ctx.alg = 0;
1828 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001829 }
1830 else
1831#endif /* MBEDTLS_MD_C */
1832 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001833 if( ! PSA_ALG_IS_MAC( alg ) )
1834 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001835 }
1836
1837 if( status != PSA_SUCCESS )
1838 memset( operation, 0, sizeof( *operation ) );
1839 return( status );
1840}
1841
Gilles Peskine01126fa2018-07-12 17:04:55 +02001842#if defined(MBEDTLS_MD_C)
1843static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1844{
Gilles Peskine3f108122018-12-07 18:14:53 +01001845 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001846 return( psa_hash_abort( &hmac->hash_ctx ) );
1847}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01001848
1849static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
1850{
1851 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
1852 memset( hmac, 0, sizeof( *hmac ) );
1853}
Gilles Peskine01126fa2018-07-12 17:04:55 +02001854#endif /* MBEDTLS_MD_C */
1855
Gilles Peskine8c9def32018-02-08 10:02:12 +01001856psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1857{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001858 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001859 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001860 /* The object has (apparently) been initialized but it is not
1861 * in use. It's ok to call abort on such an object, and there's
1862 * nothing to do. */
1863 return( PSA_SUCCESS );
1864 }
1865 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001866#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001867 if( operation->alg == PSA_ALG_CMAC )
1868 {
1869 mbedtls_cipher_free( &operation->ctx.cmac );
1870 }
1871 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001872#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001873#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001874 if( PSA_ALG_IS_HMAC( operation->alg ) )
1875 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001876 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001877 }
1878 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001879#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001880 {
1881 /* Sanity check (shouldn't happen: operation->alg should
1882 * always have been initialized to a valid value). */
1883 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001884 }
Moran Peker41deec42018-04-04 15:43:05 +03001885
Gilles Peskine8c9def32018-02-08 10:02:12 +01001886 operation->alg = 0;
1887 operation->key_set = 0;
1888 operation->iv_set = 0;
1889 operation->iv_required = 0;
1890 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001891 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001892
Gilles Peskine8c9def32018-02-08 10:02:12 +01001893 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001894
1895bad_state:
1896 /* If abort is called on an uninitialized object, we can't trust
1897 * anything. Wipe the object in case it contains confidential data.
1898 * This may result in a memory leak if a pointer gets overwritten,
1899 * but it's too late to do anything about this. */
1900 memset( operation, 0, sizeof( *operation ) );
1901 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001902}
1903
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001904#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001905static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001906 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001907 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001908 const mbedtls_cipher_info_t *cipher_info )
1909{
1910 int ret;
1911
1912 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001913
1914 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1915 if( ret != 0 )
1916 return( ret );
1917
1918 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1919 slot->data.raw.data,
1920 key_bits );
1921 return( ret );
1922}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001923#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001924
Gilles Peskine248051a2018-06-20 16:09:38 +02001925#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001926static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1927 const uint8_t *key,
1928 size_t key_length,
1929 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001930{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001931 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001932 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001933 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001934 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001935 psa_status_t status;
1936
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001937 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1938 * overflow below. This should never trigger if the hash algorithm
1939 * is implemented correctly. */
1940 /* The size checks against the ipad and opad buffers cannot be written
1941 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1942 * because that triggers -Wlogical-op on GCC 7.3. */
1943 if( block_size > sizeof( ipad ) )
1944 return( PSA_ERROR_NOT_SUPPORTED );
1945 if( block_size > sizeof( hmac->opad ) )
1946 return( PSA_ERROR_NOT_SUPPORTED );
1947 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001948 return( PSA_ERROR_NOT_SUPPORTED );
1949
Gilles Peskined223b522018-06-11 18:12:58 +02001950 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001951 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001952 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001953 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001954 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001955 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001956 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001957 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001958 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001959 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001960 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001961 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001962 }
Gilles Peskine96889972018-07-12 17:07:03 +02001963 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1964 * but it is permitted. It is common when HMAC is used in HKDF, for
1965 * example. Don't call `memcpy` in the 0-length because `key` could be
1966 * an invalid pointer which would make the behavior undefined. */
1967 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001968 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001969
Gilles Peskined223b522018-06-11 18:12:58 +02001970 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1971 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001972 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001973 ipad[i] ^= 0x36;
1974 memset( ipad + key_length, 0x36, block_size - key_length );
1975
1976 /* Copy the key material from ipad to opad, flipping the requisite bits,
1977 * and filling the rest of opad with the requisite constant. */
1978 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001979 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1980 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001981
Gilles Peskine01126fa2018-07-12 17:04:55 +02001982 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001983 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001984 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001985
Gilles Peskine01126fa2018-07-12 17:04:55 +02001986 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001987
1988cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01001989 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001990
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001991 return( status );
1992}
Gilles Peskine248051a2018-06-20 16:09:38 +02001993#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001994
Gilles Peskine89167cb2018-07-08 20:12:23 +02001995static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001996 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001997 psa_algorithm_t alg,
1998 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001999{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002000 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002001 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002002 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002003 psa_key_usage_t usage =
2004 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02002005 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002006 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002007
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002008 /* A context must be freshly initialized before it can be set up. */
2009 if( operation->alg != 0 )
2010 {
2011 return( PSA_ERROR_BAD_STATE );
2012 }
2013
Gilles Peskined911eb72018-08-14 15:18:45 +02002014 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002015 if( status != PSA_SUCCESS )
2016 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002017 if( is_sign )
2018 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002019
Gilles Peskinec5487a82018-12-03 18:08:14 +01002020 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002021 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002022 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002023 key_bits = psa_get_key_bits( slot );
2024
Gilles Peskine8c9def32018-02-08 10:02:12 +01002025#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002026 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002027 {
2028 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002029 mbedtls_cipher_info_from_psa( full_length_alg,
2030 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002031 int ret;
2032 if( cipher_info == NULL )
2033 {
2034 status = PSA_ERROR_NOT_SUPPORTED;
2035 goto exit;
2036 }
2037 operation->mac_size = cipher_info->block_size;
2038 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2039 status = mbedtls_to_psa_error( ret );
2040 }
2041 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002042#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002043#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002044 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002045 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002046 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002047 if( hash_alg == 0 )
2048 {
2049 status = PSA_ERROR_NOT_SUPPORTED;
2050 goto exit;
2051 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002052
2053 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2054 /* Sanity check. This shouldn't fail on a valid configuration. */
2055 if( operation->mac_size == 0 ||
2056 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2057 {
2058 status = PSA_ERROR_NOT_SUPPORTED;
2059 goto exit;
2060 }
2061
Gilles Peskine01126fa2018-07-12 17:04:55 +02002062 if( slot->type != PSA_KEY_TYPE_HMAC )
2063 {
2064 status = PSA_ERROR_INVALID_ARGUMENT;
2065 goto exit;
2066 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002067
Gilles Peskine01126fa2018-07-12 17:04:55 +02002068 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2069 slot->data.raw.data,
2070 slot->data.raw.bytes,
2071 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002072 }
2073 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002074#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002075 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002076 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002077 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002078 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002079
Gilles Peskined911eb72018-08-14 15:18:45 +02002080 if( truncated == 0 )
2081 {
2082 /* The "normal" case: untruncated algorithm. Nothing to do. */
2083 }
2084 else if( truncated < 4 )
2085 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002086 /* A very short MAC is too short for security since it can be
2087 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2088 * so we make this our minimum, even though 32 bits is still
2089 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002090 status = PSA_ERROR_NOT_SUPPORTED;
2091 }
2092 else if( truncated > operation->mac_size )
2093 {
2094 /* It's impossible to "truncate" to a larger length. */
2095 status = PSA_ERROR_INVALID_ARGUMENT;
2096 }
2097 else
2098 operation->mac_size = truncated;
2099
Gilles Peskinefbfac682018-07-08 20:51:54 +02002100exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002101 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002102 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002103 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002104 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002105 else
2106 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002107 operation->key_set = 1;
2108 }
2109 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002110}
2111
Gilles Peskine89167cb2018-07-08 20:12:23 +02002112psa_status_t psa_mac_sign_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, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002117}
2118
2119psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002120 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002121 psa_algorithm_t alg )
2122{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002123 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002124}
2125
Gilles Peskine8c9def32018-02-08 10:02:12 +01002126psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2127 const uint8_t *input,
2128 size_t input_length )
2129{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002130 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002131 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002132 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002133 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002134 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002135 operation->has_input = 1;
2136
Gilles Peskine8c9def32018-02-08 10:02:12 +01002137#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002138 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002139 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002140 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2141 input, input_length );
2142 status = mbedtls_to_psa_error( ret );
2143 }
2144 else
2145#endif /* MBEDTLS_CMAC_C */
2146#if defined(MBEDTLS_MD_C)
2147 if( PSA_ALG_IS_HMAC( operation->alg ) )
2148 {
2149 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2150 input_length );
2151 }
2152 else
2153#endif /* MBEDTLS_MD_C */
2154 {
2155 /* This shouldn't happen if `operation` was initialized by
2156 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002157 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002158 }
2159
Gilles Peskinefbfac682018-07-08 20:51:54 +02002160 if( status != PSA_SUCCESS )
2161 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002162 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002163}
2164
Gilles Peskine01126fa2018-07-12 17:04:55 +02002165#if defined(MBEDTLS_MD_C)
2166static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2167 uint8_t *mac,
2168 size_t mac_size )
2169{
2170 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2171 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2172 size_t hash_size = 0;
2173 size_t block_size = psa_get_hash_block_size( hash_alg );
2174 psa_status_t status;
2175
Gilles Peskine01126fa2018-07-12 17:04:55 +02002176 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2177 if( status != PSA_SUCCESS )
2178 return( status );
2179 /* From here on, tmp needs to be wiped. */
2180
2181 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2182 if( status != PSA_SUCCESS )
2183 goto exit;
2184
2185 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2186 if( status != PSA_SUCCESS )
2187 goto exit;
2188
2189 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2190 if( status != PSA_SUCCESS )
2191 goto exit;
2192
Gilles Peskined911eb72018-08-14 15:18:45 +02002193 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2194 if( status != PSA_SUCCESS )
2195 goto exit;
2196
2197 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002198
2199exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002200 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002201 return( status );
2202}
2203#endif /* MBEDTLS_MD_C */
2204
mohammad16036df908f2018-04-02 08:34:15 -07002205static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002206 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002207 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002208{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002209 if( ! operation->key_set )
2210 return( PSA_ERROR_BAD_STATE );
2211 if( operation->iv_required && ! operation->iv_set )
2212 return( PSA_ERROR_BAD_STATE );
2213
Gilles Peskine8c9def32018-02-08 10:02:12 +01002214 if( mac_size < operation->mac_size )
2215 return( PSA_ERROR_BUFFER_TOO_SMALL );
2216
Gilles Peskine8c9def32018-02-08 10:02:12 +01002217#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002218 if( operation->alg == PSA_ALG_CMAC )
2219 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002220 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2221 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2222 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002223 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002224 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002225 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002226 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002227 else
2228#endif /* MBEDTLS_CMAC_C */
2229#if defined(MBEDTLS_MD_C)
2230 if( PSA_ALG_IS_HMAC( operation->alg ) )
2231 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002232 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002233 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002234 }
2235 else
2236#endif /* MBEDTLS_MD_C */
2237 {
2238 /* This shouldn't happen if `operation` was initialized by
2239 * a setup function. */
2240 return( PSA_ERROR_BAD_STATE );
2241 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002242}
2243
Gilles Peskineacd4be32018-07-08 19:56:25 +02002244psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2245 uint8_t *mac,
2246 size_t mac_size,
2247 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002248{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002249 psa_status_t status;
2250
Jaeden Amero252ef282019-02-15 14:05:35 +00002251 if( operation->alg == 0 )
2252 {
2253 return( PSA_ERROR_BAD_STATE );
2254 }
2255
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002256 /* Fill the output buffer with something that isn't a valid mac
2257 * (barring an attack on the mac and deliberately-crafted input),
2258 * in case the caller doesn't check the return status properly. */
2259 *mac_length = mac_size;
2260 /* If mac_size is 0 then mac may be NULL and then the
2261 * call to memset would have undefined behavior. */
2262 if( mac_size != 0 )
2263 memset( mac, '!', mac_size );
2264
Gilles Peskine89167cb2018-07-08 20:12:23 +02002265 if( ! operation->is_sign )
2266 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002267 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002268 }
mohammad16036df908f2018-04-02 08:34:15 -07002269
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002270 status = psa_mac_finish_internal( operation, mac, mac_size );
2271
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002272 if( status == PSA_SUCCESS )
2273 {
2274 status = psa_mac_abort( operation );
2275 if( status == PSA_SUCCESS )
2276 *mac_length = operation->mac_size;
2277 else
2278 memset( mac, '!', mac_size );
2279 }
2280 else
2281 psa_mac_abort( operation );
2282 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002283}
2284
Gilles Peskineacd4be32018-07-08 19:56:25 +02002285psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2286 const uint8_t *mac,
2287 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002288{
Gilles Peskine828ed142018-06-18 23:25:51 +02002289 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002290 psa_status_t status;
2291
Jaeden Amero252ef282019-02-15 14:05:35 +00002292 if( operation->alg == 0 )
2293 {
2294 return( PSA_ERROR_BAD_STATE );
2295 }
2296
Gilles Peskine89167cb2018-07-08 20:12:23 +02002297 if( operation->is_sign )
2298 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002299 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002300 }
2301 if( operation->mac_size != mac_length )
2302 {
2303 status = PSA_ERROR_INVALID_SIGNATURE;
2304 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002305 }
mohammad16036df908f2018-04-02 08:34:15 -07002306
2307 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002308 actual_mac, sizeof( actual_mac ) );
2309
2310 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2311 status = PSA_ERROR_INVALID_SIGNATURE;
2312
2313cleanup:
2314 if( status == PSA_SUCCESS )
2315 status = psa_mac_abort( operation );
2316 else
2317 psa_mac_abort( operation );
2318
Gilles Peskine3f108122018-12-07 18:14:53 +01002319 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002320
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002321 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002322}
2323
2324
Gilles Peskine20035e32018-02-03 22:44:14 +01002325
Gilles Peskine20035e32018-02-03 22:44:14 +01002326/****************************************************************/
2327/* Asymmetric cryptography */
2328/****************************************************************/
2329
Gilles Peskine2b450e32018-06-27 15:42:46 +02002330#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002331/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002332 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002333static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2334 size_t hash_length,
2335 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002336{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002337 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002338 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002339 *md_alg = mbedtls_md_get_type( md_info );
2340
2341 /* The Mbed TLS RSA module uses an unsigned int for hash length
2342 * parameters. Validate that it fits so that we don't risk an
2343 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002344#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002345 if( hash_length > UINT_MAX )
2346 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002347#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002348
2349#if defined(MBEDTLS_PKCS1_V15)
2350 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2351 * must be correct. */
2352 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2353 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002354 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002355 if( md_info == NULL )
2356 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002357 if( mbedtls_md_get_size( md_info ) != hash_length )
2358 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002359 }
2360#endif /* MBEDTLS_PKCS1_V15 */
2361
2362#if defined(MBEDTLS_PKCS1_V21)
2363 /* PSS requires a hash internally. */
2364 if( PSA_ALG_IS_RSA_PSS( alg ) )
2365 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002366 if( md_info == NULL )
2367 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002368 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002369#endif /* MBEDTLS_PKCS1_V21 */
2370
Gilles Peskine61b91d42018-06-08 16:09:36 +02002371 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002372}
2373
Gilles Peskine2b450e32018-06-27 15:42:46 +02002374static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2375 psa_algorithm_t alg,
2376 const uint8_t *hash,
2377 size_t hash_length,
2378 uint8_t *signature,
2379 size_t signature_size,
2380 size_t *signature_length )
2381{
2382 psa_status_t status;
2383 int ret;
2384 mbedtls_md_type_t md_alg;
2385
2386 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2387 if( status != PSA_SUCCESS )
2388 return( status );
2389
Gilles Peskine630a18a2018-06-29 17:49:35 +02002390 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002391 return( PSA_ERROR_BUFFER_TOO_SMALL );
2392
2393#if defined(MBEDTLS_PKCS1_V15)
2394 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2395 {
2396 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2397 MBEDTLS_MD_NONE );
2398 ret = mbedtls_rsa_pkcs1_sign( rsa,
2399 mbedtls_ctr_drbg_random,
2400 &global_data.ctr_drbg,
2401 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002402 md_alg,
2403 (unsigned int) hash_length,
2404 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002405 signature );
2406 }
2407 else
2408#endif /* MBEDTLS_PKCS1_V15 */
2409#if defined(MBEDTLS_PKCS1_V21)
2410 if( PSA_ALG_IS_RSA_PSS( alg ) )
2411 {
2412 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2413 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2414 mbedtls_ctr_drbg_random,
2415 &global_data.ctr_drbg,
2416 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002417 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002418 (unsigned int) hash_length,
2419 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002420 signature );
2421 }
2422 else
2423#endif /* MBEDTLS_PKCS1_V21 */
2424 {
2425 return( PSA_ERROR_INVALID_ARGUMENT );
2426 }
2427
2428 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002429 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002430 return( mbedtls_to_psa_error( ret ) );
2431}
2432
2433static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2434 psa_algorithm_t alg,
2435 const uint8_t *hash,
2436 size_t hash_length,
2437 const uint8_t *signature,
2438 size_t signature_length )
2439{
2440 psa_status_t status;
2441 int ret;
2442 mbedtls_md_type_t md_alg;
2443
2444 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2445 if( status != PSA_SUCCESS )
2446 return( status );
2447
Gilles Peskine630a18a2018-06-29 17:49:35 +02002448 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002449 return( PSA_ERROR_BUFFER_TOO_SMALL );
2450
2451#if defined(MBEDTLS_PKCS1_V15)
2452 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2453 {
2454 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2455 MBEDTLS_MD_NONE );
2456 ret = mbedtls_rsa_pkcs1_verify( rsa,
2457 mbedtls_ctr_drbg_random,
2458 &global_data.ctr_drbg,
2459 MBEDTLS_RSA_PUBLIC,
2460 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002461 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002462 hash,
2463 signature );
2464 }
2465 else
2466#endif /* MBEDTLS_PKCS1_V15 */
2467#if defined(MBEDTLS_PKCS1_V21)
2468 if( PSA_ALG_IS_RSA_PSS( alg ) )
2469 {
2470 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2471 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2472 mbedtls_ctr_drbg_random,
2473 &global_data.ctr_drbg,
2474 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002475 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002476 (unsigned int) hash_length,
2477 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002478 signature );
2479 }
2480 else
2481#endif /* MBEDTLS_PKCS1_V21 */
2482 {
2483 return( PSA_ERROR_INVALID_ARGUMENT );
2484 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002485
2486 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2487 * the rest of the signature is invalid". This has little use in
2488 * practice and PSA doesn't report this distinction. */
2489 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2490 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002491 return( mbedtls_to_psa_error( ret ) );
2492}
2493#endif /* MBEDTLS_RSA_C */
2494
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002495#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002496/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2497 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2498 * (even though these functions don't modify it). */
2499static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2500 psa_algorithm_t alg,
2501 const uint8_t *hash,
2502 size_t hash_length,
2503 uint8_t *signature,
2504 size_t signature_size,
2505 size_t *signature_length )
2506{
2507 int ret;
2508 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002509 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002510 mbedtls_mpi_init( &r );
2511 mbedtls_mpi_init( &s );
2512
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002513 if( signature_size < 2 * curve_bytes )
2514 {
2515 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2516 goto cleanup;
2517 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002518
Gilles Peskinea05219c2018-11-16 16:02:56 +01002519#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002520 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2521 {
2522 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2523 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2524 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2525 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2526 hash, hash_length,
2527 md_alg ) );
2528 }
2529 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002530#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002531 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002532 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002533 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2534 hash, hash_length,
2535 mbedtls_ctr_drbg_random,
2536 &global_data.ctr_drbg ) );
2537 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002538
2539 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2540 signature,
2541 curve_bytes ) );
2542 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2543 signature + curve_bytes,
2544 curve_bytes ) );
2545
2546cleanup:
2547 mbedtls_mpi_free( &r );
2548 mbedtls_mpi_free( &s );
2549 if( ret == 0 )
2550 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002551 return( mbedtls_to_psa_error( ret ) );
2552}
2553
2554static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2555 const uint8_t *hash,
2556 size_t hash_length,
2557 const uint8_t *signature,
2558 size_t signature_length )
2559{
2560 int ret;
2561 mbedtls_mpi r, s;
2562 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2563 mbedtls_mpi_init( &r );
2564 mbedtls_mpi_init( &s );
2565
2566 if( signature_length != 2 * curve_bytes )
2567 return( PSA_ERROR_INVALID_SIGNATURE );
2568
2569 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2570 signature,
2571 curve_bytes ) );
2572 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2573 signature + curve_bytes,
2574 curve_bytes ) );
2575
2576 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2577 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002578
2579cleanup:
2580 mbedtls_mpi_free( &r );
2581 mbedtls_mpi_free( &s );
2582 return( mbedtls_to_psa_error( ret ) );
2583}
2584#endif /* MBEDTLS_ECDSA_C */
2585
Gilles Peskinec5487a82018-12-03 18:08:14 +01002586psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002587 psa_algorithm_t alg,
2588 const uint8_t *hash,
2589 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002590 uint8_t *signature,
2591 size_t signature_size,
2592 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002593{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002594 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002595 psa_status_t status;
2596
2597 *signature_length = signature_size;
2598
Gilles Peskinec5487a82018-12-03 18:08:14 +01002599 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002600 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002601 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002602 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002603 {
2604 status = PSA_ERROR_INVALID_ARGUMENT;
2605 goto exit;
2606 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002607
Gilles Peskine20035e32018-02-03 22:44:14 +01002608#if defined(MBEDTLS_RSA_C)
2609 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2610 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002611 status = psa_rsa_sign( slot->data.rsa,
2612 alg,
2613 hash, hash_length,
2614 signature, signature_size,
2615 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002616 }
2617 else
2618#endif /* defined(MBEDTLS_RSA_C) */
2619#if defined(MBEDTLS_ECP_C)
2620 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2621 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002622#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002623 if(
2624#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2625 PSA_ALG_IS_ECDSA( alg )
2626#else
2627 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2628#endif
2629 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002630 status = psa_ecdsa_sign( slot->data.ecp,
2631 alg,
2632 hash, hash_length,
2633 signature, signature_size,
2634 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002635 else
2636#endif /* defined(MBEDTLS_ECDSA_C) */
2637 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002638 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002639 }
itayzafrir5c753392018-05-08 11:18:38 +03002640 }
2641 else
2642#endif /* defined(MBEDTLS_ECP_C) */
2643 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002644 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002645 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002646
2647exit:
2648 /* Fill the unused part of the output buffer (the whole buffer on error,
2649 * the trailing part on success) with something that isn't a valid mac
2650 * (barring an attack on the mac and deliberately-crafted input),
2651 * in case the caller doesn't check the return status properly. */
2652 if( status == PSA_SUCCESS )
2653 memset( signature + *signature_length, '!',
2654 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002655 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002656 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002657 /* If signature_size is 0 then we have nothing to do. We must not call
2658 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002659 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002660}
2661
Gilles Peskinec5487a82018-12-03 18:08:14 +01002662psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03002663 psa_algorithm_t alg,
2664 const uint8_t *hash,
2665 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002666 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002667 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002668{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002669 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002670 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002671
Gilles Peskinec5487a82018-12-03 18:08:14 +01002672 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002673 if( status != PSA_SUCCESS )
2674 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002675
Gilles Peskine61b91d42018-06-08 16:09:36 +02002676#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002677 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002678 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002679 return( psa_rsa_verify( slot->data.rsa,
2680 alg,
2681 hash, hash_length,
2682 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002683 }
2684 else
2685#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002686#if defined(MBEDTLS_ECP_C)
2687 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2688 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002689#if defined(MBEDTLS_ECDSA_C)
2690 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002691 return( psa_ecdsa_verify( slot->data.ecp,
2692 hash, hash_length,
2693 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002694 else
2695#endif /* defined(MBEDTLS_ECDSA_C) */
2696 {
2697 return( PSA_ERROR_INVALID_ARGUMENT );
2698 }
itayzafrir5c753392018-05-08 11:18:38 +03002699 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002700 else
2701#endif /* defined(MBEDTLS_ECP_C) */
2702 {
2703 return( PSA_ERROR_NOT_SUPPORTED );
2704 }
2705}
2706
Gilles Peskine072ac562018-06-30 00:21:29 +02002707#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2708static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2709 mbedtls_rsa_context *rsa )
2710{
2711 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2712 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2713 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2714 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2715}
2716#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2717
Gilles Peskinec5487a82018-12-03 18:08:14 +01002718psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002719 psa_algorithm_t alg,
2720 const uint8_t *input,
2721 size_t input_length,
2722 const uint8_t *salt,
2723 size_t salt_length,
2724 uint8_t *output,
2725 size_t output_size,
2726 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002727{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002728 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002729 psa_status_t status;
2730
Darryl Green5cc689a2018-07-24 15:34:10 +01002731 (void) input;
2732 (void) input_length;
2733 (void) salt;
2734 (void) output;
2735 (void) output_size;
2736
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002737 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002738
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002739 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2740 return( PSA_ERROR_INVALID_ARGUMENT );
2741
Gilles Peskinec5487a82018-12-03 18:08:14 +01002742 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002743 if( status != PSA_SUCCESS )
2744 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002745 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2746 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002747 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002748
2749#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002750 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002751 {
2752 mbedtls_rsa_context *rsa = slot->data.rsa;
2753 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002754 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002755 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002756#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002757 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002758 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002759 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2760 mbedtls_ctr_drbg_random,
2761 &global_data.ctr_drbg,
2762 MBEDTLS_RSA_PUBLIC,
2763 input_length,
2764 input,
2765 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002766 }
2767 else
2768#endif /* MBEDTLS_PKCS1_V15 */
2769#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002770 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002771 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002772 psa_rsa_oaep_set_padding_mode( alg, rsa );
2773 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2774 mbedtls_ctr_drbg_random,
2775 &global_data.ctr_drbg,
2776 MBEDTLS_RSA_PUBLIC,
2777 salt, salt_length,
2778 input_length,
2779 input,
2780 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002781 }
2782 else
2783#endif /* MBEDTLS_PKCS1_V21 */
2784 {
2785 return( PSA_ERROR_INVALID_ARGUMENT );
2786 }
2787 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002788 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002789 return( mbedtls_to_psa_error( ret ) );
2790 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002791 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002792#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002793 {
2794 return( PSA_ERROR_NOT_SUPPORTED );
2795 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002796}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002797
Gilles Peskinec5487a82018-12-03 18:08:14 +01002798psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002799 psa_algorithm_t alg,
2800 const uint8_t *input,
2801 size_t input_length,
2802 const uint8_t *salt,
2803 size_t salt_length,
2804 uint8_t *output,
2805 size_t output_size,
2806 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002807{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002808 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002809 psa_status_t status;
2810
Darryl Green5cc689a2018-07-24 15:34:10 +01002811 (void) input;
2812 (void) input_length;
2813 (void) salt;
2814 (void) output;
2815 (void) output_size;
2816
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002817 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002818
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002819 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2820 return( PSA_ERROR_INVALID_ARGUMENT );
2821
Gilles Peskinec5487a82018-12-03 18:08:14 +01002822 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002823 if( status != PSA_SUCCESS )
2824 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002825 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2826 return( PSA_ERROR_INVALID_ARGUMENT );
2827
2828#if defined(MBEDTLS_RSA_C)
2829 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2830 {
2831 mbedtls_rsa_context *rsa = slot->data.rsa;
2832 int ret;
2833
Gilles Peskine630a18a2018-06-29 17:49:35 +02002834 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002835 return( PSA_ERROR_INVALID_ARGUMENT );
2836
2837#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002838 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002839 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002840 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2841 mbedtls_ctr_drbg_random,
2842 &global_data.ctr_drbg,
2843 MBEDTLS_RSA_PRIVATE,
2844 output_length,
2845 input,
2846 output,
2847 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002848 }
2849 else
2850#endif /* MBEDTLS_PKCS1_V15 */
2851#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002852 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002853 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002854 psa_rsa_oaep_set_padding_mode( alg, rsa );
2855 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2856 mbedtls_ctr_drbg_random,
2857 &global_data.ctr_drbg,
2858 MBEDTLS_RSA_PRIVATE,
2859 salt, salt_length,
2860 output_length,
2861 input,
2862 output,
2863 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002864 }
2865 else
2866#endif /* MBEDTLS_PKCS1_V21 */
2867 {
2868 return( PSA_ERROR_INVALID_ARGUMENT );
2869 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002870
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002871 return( mbedtls_to_psa_error( ret ) );
2872 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002873 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002874#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002875 {
2876 return( PSA_ERROR_NOT_SUPPORTED );
2877 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002878}
Gilles Peskine20035e32018-02-03 22:44:14 +01002879
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002880
2881
mohammad1603503973b2018-03-12 15:59:30 +02002882/****************************************************************/
2883/* Symmetric cryptography */
2884/****************************************************************/
2885
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002886/* Initialize the cipher operation structure. Once this function has been
2887 * called, psa_cipher_abort can run and will do the right thing. */
2888static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2889 psa_algorithm_t alg )
2890{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002891 if( ! PSA_ALG_IS_CIPHER( alg ) )
2892 {
2893 memset( operation, 0, sizeof( *operation ) );
2894 return( PSA_ERROR_INVALID_ARGUMENT );
2895 }
2896
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002897 operation->alg = alg;
2898 operation->key_set = 0;
2899 operation->iv_set = 0;
2900 operation->iv_required = 1;
2901 operation->iv_size = 0;
2902 operation->block_size = 0;
2903 mbedtls_cipher_init( &operation->ctx.cipher );
2904 return( PSA_SUCCESS );
2905}
2906
Gilles Peskinee553c652018-06-04 16:22:46 +02002907static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002908 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02002909 psa_algorithm_t alg,
2910 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002911{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01002912 int ret = 0;
2913 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002914 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002915 size_t key_bits;
2916 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002917 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2918 PSA_KEY_USAGE_ENCRYPT :
2919 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002920
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002921 /* A context must be freshly initialized before it can be set up. */
2922 if( operation->alg != 0 )
2923 {
2924 return( PSA_ERROR_BAD_STATE );
2925 }
2926
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002927 status = psa_cipher_init( operation, alg );
2928 if( status != PSA_SUCCESS )
2929 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002930
Gilles Peskinec5487a82018-12-03 18:08:14 +01002931 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002932 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01002933 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002934 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002935
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002936 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002937 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01002938 {
2939 status = PSA_ERROR_NOT_SUPPORTED;
2940 goto exit;
2941 }
mohammad1603503973b2018-03-12 15:59:30 +02002942
mohammad1603503973b2018-03-12 15:59:30 +02002943 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002944 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01002945 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002946
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002947#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002948 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002949 {
2950 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2951 unsigned char keys[24];
2952 memcpy( keys, slot->data.raw.data, 16 );
2953 memcpy( keys + 16, slot->data.raw.data, 8 );
2954 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2955 keys,
2956 192, cipher_operation );
2957 }
2958 else
2959#endif
2960 {
2961 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2962 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002963 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002964 }
Moran Peker41deec42018-04-04 15:43:05 +03002965 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01002966 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002967
mohammad16038481e742018-03-18 13:57:31 +02002968#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002969 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002970 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002971 case PSA_ALG_CBC_NO_PADDING:
2972 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2973 MBEDTLS_PADDING_NONE );
2974 break;
2975 case PSA_ALG_CBC_PKCS7:
2976 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2977 MBEDTLS_PADDING_PKCS7 );
2978 break;
2979 default:
2980 /* The algorithm doesn't involve padding. */
2981 ret = 0;
2982 break;
2983 }
2984 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01002985 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02002986#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2987
mohammad1603503973b2018-03-12 15:59:30 +02002988 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002989 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2990 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
2991 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02002992 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002993 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002994 }
mohammad1603503973b2018-03-12 15:59:30 +02002995
Gilles Peskine9ab61b62019-02-25 17:43:14 +01002996exit:
2997 if( status == 0 )
2998 status = mbedtls_to_psa_error( ret );
2999 if( status != 0 )
3000 psa_cipher_abort( operation );
3001 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003002}
3003
Gilles Peskinefe119512018-07-08 21:39:34 +02003004psa_status_t psa_cipher_encrypt_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_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003009}
3010
Gilles Peskinefe119512018-07-08 21:39:34 +02003011psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003012 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003013 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003014{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003015 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003016}
3017
Gilles Peskinefe119512018-07-08 21:39:34 +02003018psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
3019 unsigned char *iv,
3020 size_t iv_size,
3021 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003022{
itayzafrir534bd7c2018-08-02 13:56:32 +03003023 psa_status_t status;
3024 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003025 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003026 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003027 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003028 }
Moran Peker41deec42018-04-04 15:43:05 +03003029 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003030 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003031 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003032 goto exit;
3033 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003034 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3035 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003036 if( ret != 0 )
3037 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003038 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003039 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003040 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003041
mohammad16038481e742018-03-18 13:57:31 +02003042 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003043 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003044
Moran Peker395db872018-05-31 14:07:14 +03003045exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003046 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003047 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003048 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003049}
3050
Gilles Peskinefe119512018-07-08 21:39:34 +02003051psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
3052 const unsigned char *iv,
3053 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003054{
itayzafrir534bd7c2018-08-02 13:56:32 +03003055 psa_status_t status;
3056 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003057 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003058 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003059 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003060 }
Moran Pekera28258c2018-05-29 16:25:04 +03003061 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003062 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003063 status = PSA_ERROR_INVALID_ARGUMENT;
3064 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003065 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003066 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3067 status = mbedtls_to_psa_error( ret );
3068exit:
3069 if( status == PSA_SUCCESS )
3070 operation->iv_set = 1;
3071 else
mohammad1603503973b2018-03-12 15:59:30 +02003072 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003073 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003074}
3075
Gilles Peskinee553c652018-06-04 16:22:46 +02003076psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3077 const uint8_t *input,
3078 size_t input_length,
3079 unsigned char *output,
3080 size_t output_size,
3081 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003082{
itayzafrir534bd7c2018-08-02 13:56:32 +03003083 psa_status_t status;
3084 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003085 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003086
3087 if( operation->alg == 0 )
3088 {
3089 return( PSA_ERROR_BAD_STATE );
3090 }
3091
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003092 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003093 {
3094 /* Take the unprocessed partial block left over from previous
3095 * update calls, if any, plus the input to this call. Remove
3096 * the last partial block, if any. You get the data that will be
3097 * output in this call. */
3098 expected_output_size =
3099 ( operation->ctx.cipher.unprocessed_len + input_length )
3100 / operation->block_size * operation->block_size;
3101 }
3102 else
3103 {
3104 expected_output_size = input_length;
3105 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003106
Gilles Peskine89d789c2018-06-04 17:17:16 +02003107 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003108 {
3109 status = PSA_ERROR_BUFFER_TOO_SMALL;
3110 goto exit;
3111 }
mohammad160382759612018-03-12 18:16:40 +02003112
mohammad1603503973b2018-03-12 15:59:30 +02003113 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003114 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003115 status = mbedtls_to_psa_error( ret );
3116exit:
3117 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003118 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003119 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003120}
3121
Gilles Peskinee553c652018-06-04 16:22:46 +02003122psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3123 uint8_t *output,
3124 size_t output_size,
3125 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003126{
David Saadab4ecc272019-02-14 13:48:10 +02003127 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003128 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003129 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003130
mohammad1603503973b2018-03-12 15:59:30 +02003131 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003132 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003133 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003134 }
3135 if( operation->iv_required && ! operation->iv_set )
3136 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003137 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003138 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003139
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003140 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003141 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3142 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003143 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003144 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003145 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003146 }
3147
Janos Follath315b51c2018-07-09 16:04:51 +01003148 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3149 temp_output_buffer,
3150 output_length );
3151 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003152 {
Janos Follath315b51c2018-07-09 16:04:51 +01003153 status = mbedtls_to_psa_error( cipher_ret );
3154 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003155 }
Janos Follath315b51c2018-07-09 16:04:51 +01003156
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003157 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003158 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003159 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003160 memcpy( output, temp_output_buffer, *output_length );
3161 else
3162 {
Janos Follath315b51c2018-07-09 16:04:51 +01003163 status = PSA_ERROR_BUFFER_TOO_SMALL;
3164 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003165 }
mohammad1603503973b2018-03-12 15:59:30 +02003166
Gilles Peskine3f108122018-12-07 18:14:53 +01003167 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003168 status = psa_cipher_abort( operation );
3169
3170 return( status );
3171
3172error:
3173
3174 *output_length = 0;
3175
Gilles Peskine3f108122018-12-07 18:14:53 +01003176 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003177 (void) psa_cipher_abort( operation );
3178
3179 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003180}
3181
Gilles Peskinee553c652018-06-04 16:22:46 +02003182psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3183{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003184 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003185 {
3186 /* The object has (apparently) been initialized but it is not
3187 * in use. It's ok to call abort on such an object, and there's
3188 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003189 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003190 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003191
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003192 /* Sanity check (shouldn't happen: operation->alg should
3193 * always have been initialized to a valid value). */
3194 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3195 return( PSA_ERROR_BAD_STATE );
3196
mohammad1603503973b2018-03-12 15:59:30 +02003197 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003198
Moran Peker41deec42018-04-04 15:43:05 +03003199 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003200 operation->key_set = 0;
3201 operation->iv_set = 0;
3202 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003203 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003204 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003205
Moran Peker395db872018-05-31 14:07:14 +03003206 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003207}
3208
Gilles Peskinea0655c32018-04-30 17:06:50 +02003209
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003210
mohammad16038cc1cee2018-03-28 01:21:33 +03003211/****************************************************************/
3212/* Key Policy */
3213/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003214
mohammad160327010052018-07-03 13:16:15 +03003215#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02003216void psa_key_policy_set_usage( psa_key_policy_t *policy,
3217 psa_key_usage_t usage,
3218 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03003219{
mohammad16034eed7572018-03-28 05:14:59 -07003220 policy->usage = usage;
3221 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03003222}
3223
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003224psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003225{
mohammad16036df908f2018-04-02 08:34:15 -07003226 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03003227}
3228
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003229psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003230{
mohammad16036df908f2018-04-02 08:34:15 -07003231 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03003232}
Gilles Peskined6f371b2019-05-10 19:33:38 +02003233
3234void psa_key_policy_set_enrollment_algorithm( psa_key_policy_t *policy,
3235 psa_algorithm_t alg2 )
3236{
3237 policy->alg2 = alg2;
3238}
3239
3240psa_algorithm_t psa_key_policy_get_enrollment_algorithm(
3241 const psa_key_policy_t *policy )
3242{
3243 return( policy->alg2 );
3244}
mohammad160327010052018-07-03 13:16:15 +03003245#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003246
Gilles Peskinec5487a82018-12-03 18:08:14 +01003247psa_status_t psa_set_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003248 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003249{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003250 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003251 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003252
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003253 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003254 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003255
Gilles Peskinec5487a82018-12-03 18:08:14 +01003256 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003257 if( status != PSA_SUCCESS )
3258 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03003259
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003260 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
3261 PSA_KEY_USAGE_ENCRYPT |
3262 PSA_KEY_USAGE_DECRYPT |
3263 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02003264 PSA_KEY_USAGE_VERIFY |
3265 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07003266 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03003267
mohammad16036df908f2018-04-02 08:34:15 -07003268 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003269
3270 return( PSA_SUCCESS );
3271}
3272
Gilles Peskinec5487a82018-12-03 18:08:14 +01003273psa_status_t psa_get_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003274 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003275{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003276 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003277 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003278
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003279 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003280 return( PSA_ERROR_INVALID_ARGUMENT );
3281
Gilles Peskinec5487a82018-12-03 18:08:14 +01003282 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003283 if( status != PSA_SUCCESS )
3284 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003285
mohammad16036df908f2018-04-02 08:34:15 -07003286 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003287
3288 return( PSA_SUCCESS );
3289}
Gilles Peskine20035e32018-02-03 22:44:14 +01003290
Gilles Peskinea0655c32018-04-30 17:06:50 +02003291
3292
mohammad1603804cd712018-03-20 22:44:08 +02003293/****************************************************************/
3294/* Key Lifetime */
3295/****************************************************************/
3296
Gilles Peskinec5487a82018-12-03 18:08:14 +01003297psa_status_t psa_get_key_lifetime( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003298 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003299{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003300 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003301 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003302
Gilles Peskinec5487a82018-12-03 18:08:14 +01003303 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003304 if( status != PSA_SUCCESS )
3305 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003306
mohammad1603804cd712018-03-20 22:44:08 +02003307 *lifetime = slot->lifetime;
3308
3309 return( PSA_SUCCESS );
3310}
3311
Gilles Peskine20035e32018-02-03 22:44:14 +01003312
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003313
mohammad16035955c982018-04-26 00:53:03 +03003314/****************************************************************/
3315/* AEAD */
3316/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003317
Gilles Peskineedf9a652018-08-17 18:11:56 +02003318typedef struct
3319{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003320 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003321 const mbedtls_cipher_info_t *cipher_info;
3322 union
3323 {
3324#if defined(MBEDTLS_CCM_C)
3325 mbedtls_ccm_context ccm;
3326#endif /* MBEDTLS_CCM_C */
3327#if defined(MBEDTLS_GCM_C)
3328 mbedtls_gcm_context gcm;
3329#endif /* MBEDTLS_GCM_C */
3330 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003331 psa_algorithm_t core_alg;
3332 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003333 uint8_t tag_length;
3334} aead_operation_t;
3335
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003336static void psa_aead_abort( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003337{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003338 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003339 {
3340#if defined(MBEDTLS_CCM_C)
3341 case PSA_ALG_CCM:
3342 mbedtls_ccm_free( &operation->ctx.ccm );
3343 break;
3344#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003345#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003346 case PSA_ALG_GCM:
3347 mbedtls_gcm_free( &operation->ctx.gcm );
3348 break;
3349#endif /* MBEDTLS_GCM_C */
3350 }
3351}
3352
3353static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003354 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003355 psa_key_usage_t usage,
3356 psa_algorithm_t alg )
3357{
3358 psa_status_t status;
3359 size_t key_bits;
3360 mbedtls_cipher_id_t cipher_id;
3361
Gilles Peskinec5487a82018-12-03 18:08:14 +01003362 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003363 if( status != PSA_SUCCESS )
3364 return( status );
3365
3366 key_bits = psa_get_key_bits( operation->slot );
3367
3368 operation->cipher_info =
3369 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3370 &cipher_id );
3371 if( operation->cipher_info == NULL )
3372 return( PSA_ERROR_NOT_SUPPORTED );
3373
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003374 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003375 {
3376#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003377 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3378 operation->core_alg = PSA_ALG_CCM;
3379 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003380 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3381 return( PSA_ERROR_INVALID_ARGUMENT );
3382 mbedtls_ccm_init( &operation->ctx.ccm );
3383 status = mbedtls_to_psa_error(
3384 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3385 operation->slot->data.raw.data,
3386 (unsigned int) key_bits ) );
3387 if( status != 0 )
3388 goto cleanup;
3389 break;
3390#endif /* MBEDTLS_CCM_C */
3391
3392#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003393 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3394 operation->core_alg = PSA_ALG_GCM;
3395 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003396 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3397 return( PSA_ERROR_INVALID_ARGUMENT );
3398 mbedtls_gcm_init( &operation->ctx.gcm );
3399 status = mbedtls_to_psa_error(
3400 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3401 operation->slot->data.raw.data,
3402 (unsigned int) key_bits ) );
3403 break;
3404#endif /* MBEDTLS_GCM_C */
3405
3406 default:
3407 return( PSA_ERROR_NOT_SUPPORTED );
3408 }
3409
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003410 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3411 {
3412 status = PSA_ERROR_INVALID_ARGUMENT;
3413 goto cleanup;
3414 }
3415 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3416 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3417 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3418 * In both cases, mbedtls_xxx will validate the tag length below. */
3419
Gilles Peskineedf9a652018-08-17 18:11:56 +02003420 return( PSA_SUCCESS );
3421
3422cleanup:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003423 psa_aead_abort( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003424 return( status );
3425}
3426
Gilles Peskinec5487a82018-12-03 18:08:14 +01003427psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003428 psa_algorithm_t alg,
3429 const uint8_t *nonce,
3430 size_t nonce_length,
3431 const uint8_t *additional_data,
3432 size_t additional_data_length,
3433 const uint8_t *plaintext,
3434 size_t plaintext_length,
3435 uint8_t *ciphertext,
3436 size_t ciphertext_size,
3437 size_t *ciphertext_length )
3438{
mohammad16035955c982018-04-26 00:53:03 +03003439 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003440 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003441 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003442
mohammad1603f08a5502018-06-03 15:05:47 +03003443 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003444
Gilles Peskinec5487a82018-12-03 18:08:14 +01003445 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003446 if( status != PSA_SUCCESS )
3447 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003448
Gilles Peskineedf9a652018-08-17 18:11:56 +02003449 /* For all currently supported modes, the tag is at the end of the
3450 * ciphertext. */
3451 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3452 {
3453 status = PSA_ERROR_BUFFER_TOO_SMALL;
3454 goto exit;
3455 }
3456 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003457
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003458#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003459 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003460 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003461 status = mbedtls_to_psa_error(
3462 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3463 MBEDTLS_GCM_ENCRYPT,
3464 plaintext_length,
3465 nonce, nonce_length,
3466 additional_data, additional_data_length,
3467 plaintext, ciphertext,
3468 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003469 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003470 else
3471#endif /* MBEDTLS_GCM_C */
3472#if defined(MBEDTLS_CCM_C)
3473 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003474 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003475 status = mbedtls_to_psa_error(
3476 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3477 plaintext_length,
3478 nonce, nonce_length,
3479 additional_data,
3480 additional_data_length,
3481 plaintext, ciphertext,
3482 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003483 }
mohammad16035c8845f2018-05-09 05:40:09 -07003484 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003485#endif /* MBEDTLS_CCM_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003486 {
mohammad1603554faad2018-06-03 15:07:38 +03003487 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003488 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003489
Gilles Peskineedf9a652018-08-17 18:11:56 +02003490 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3491 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003492
Gilles Peskineedf9a652018-08-17 18:11:56 +02003493exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003494 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003495 if( status == PSA_SUCCESS )
3496 *ciphertext_length = plaintext_length + operation.tag_length;
3497 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003498}
3499
Gilles Peskineee652a32018-06-01 19:23:52 +02003500/* Locate the tag in a ciphertext buffer containing the encrypted data
3501 * followed by the tag. Return the length of the part preceding the tag in
3502 * *plaintext_length. This is the size of the plaintext in modes where
3503 * the encrypted data has the same size as the plaintext, such as
3504 * CCM and GCM. */
3505static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3506 const uint8_t *ciphertext,
3507 size_t ciphertext_length,
3508 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003509 const uint8_t **p_tag )
3510{
3511 size_t payload_length;
3512 if( tag_length > ciphertext_length )
3513 return( PSA_ERROR_INVALID_ARGUMENT );
3514 payload_length = ciphertext_length - tag_length;
3515 if( payload_length > plaintext_size )
3516 return( PSA_ERROR_BUFFER_TOO_SMALL );
3517 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003518 return( PSA_SUCCESS );
3519}
3520
Gilles Peskinec5487a82018-12-03 18:08:14 +01003521psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003522 psa_algorithm_t alg,
3523 const uint8_t *nonce,
3524 size_t nonce_length,
3525 const uint8_t *additional_data,
3526 size_t additional_data_length,
3527 const uint8_t *ciphertext,
3528 size_t ciphertext_length,
3529 uint8_t *plaintext,
3530 size_t plaintext_size,
3531 size_t *plaintext_length )
3532{
mohammad16035955c982018-04-26 00:53:03 +03003533 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003534 aead_operation_t operation;
3535 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003536
Gilles Peskineee652a32018-06-01 19:23:52 +02003537 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003538
Gilles Peskinec5487a82018-12-03 18:08:14 +01003539 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003540 if( status != PSA_SUCCESS )
3541 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003542
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003543#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003544 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003545 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003546 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003547 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003548 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003549 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003550 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003551
Gilles Peskineedf9a652018-08-17 18:11:56 +02003552 status = mbedtls_to_psa_error(
3553 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3554 ciphertext_length - operation.tag_length,
3555 nonce, nonce_length,
3556 additional_data,
3557 additional_data_length,
3558 tag, operation.tag_length,
3559 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003560 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003561 else
3562#endif /* MBEDTLS_GCM_C */
3563#if defined(MBEDTLS_CCM_C)
3564 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003565 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003566 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003567 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003568 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003569 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003570 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003571
Gilles Peskineedf9a652018-08-17 18:11:56 +02003572 status = mbedtls_to_psa_error(
3573 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3574 ciphertext_length - operation.tag_length,
3575 nonce, nonce_length,
3576 additional_data,
3577 additional_data_length,
3578 ciphertext, plaintext,
3579 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003580 }
mohammad160339574652018-06-01 04:39:53 -07003581 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003582#endif /* MBEDTLS_CCM_C */
mohammad160339574652018-06-01 04:39:53 -07003583 {
mohammad1603554faad2018-06-03 15:07:38 +03003584 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003585 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003586
Gilles Peskineedf9a652018-08-17 18:11:56 +02003587 if( status != PSA_SUCCESS && plaintext_size != 0 )
3588 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003589
Gilles Peskineedf9a652018-08-17 18:11:56 +02003590exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003591 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003592 if( status == PSA_SUCCESS )
3593 *plaintext_length = ciphertext_length - operation.tag_length;
3594 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003595}
3596
Gilles Peskinea0655c32018-04-30 17:06:50 +02003597
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003598
Gilles Peskine20035e32018-02-03 22:44:14 +01003599/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003600/* Generators */
3601/****************************************************************/
3602
3603psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3604{
3605 psa_status_t status = PSA_SUCCESS;
3606 if( generator->alg == 0 )
3607 {
3608 /* The object has (apparently) been initialized but it is not
3609 * in use. It's ok to call abort on such an object, and there's
3610 * nothing to do. */
3611 }
3612 else
Gilles Peskine751d9652018-09-18 12:05:44 +02003613 if( generator->alg == PSA_ALG_SELECT_RAW )
3614 {
3615 if( generator->ctx.buffer.data != NULL )
3616 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003617 mbedtls_platform_zeroize( generator->ctx.buffer.data,
Gilles Peskine751d9652018-09-18 12:05:44 +02003618 generator->ctx.buffer.size );
3619 mbedtls_free( generator->ctx.buffer.data );
3620 }
3621 }
3622 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003623#if defined(MBEDTLS_MD_C)
3624 if( PSA_ALG_IS_HKDF( generator->alg ) )
3625 {
3626 mbedtls_free( generator->ctx.hkdf.info );
3627 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3628 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003629 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3630 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
3631 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003632 {
3633 if( generator->ctx.tls12_prf.key != NULL )
3634 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003635 mbedtls_platform_zeroize( generator->ctx.tls12_prf.key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003636 generator->ctx.tls12_prf.key_len );
3637 mbedtls_free( generator->ctx.tls12_prf.key );
3638 }
Hanno Becker580fba12018-11-13 20:50:45 +00003639
3640 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
3641 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003642 mbedtls_platform_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003643 generator->ctx.tls12_prf.Ai_with_seed_len );
3644 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
3645 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003646 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003647 else
3648#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003649 {
3650 status = PSA_ERROR_BAD_STATE;
3651 }
3652 memset( generator, 0, sizeof( *generator ) );
3653 return( status );
3654}
3655
3656
3657psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3658 size_t *capacity)
3659{
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003660 if( generator->alg == 0 )
3661 {
3662 /* This is a blank generator. */
3663 return PSA_ERROR_BAD_STATE;
3664 }
3665
Gilles Peskineeab56e42018-07-12 17:12:33 +02003666 *capacity = generator->capacity;
3667 return( PSA_SUCCESS );
3668}
3669
Gilles Peskinebef7f142018-07-12 17:22:21 +02003670#if defined(MBEDTLS_MD_C)
3671/* Read some bytes from an HKDF-based generator. This performs a chunk
3672 * of the expand phase of the HKDF algorithm. */
3673static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3674 psa_algorithm_t hash_alg,
3675 uint8_t *output,
3676 size_t output_length )
3677{
3678 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3679 psa_status_t status;
3680
3681 while( output_length != 0 )
3682 {
3683 /* Copy what remains of the current block */
3684 uint8_t n = hash_length - hkdf->offset_in_block;
3685 if( n > output_length )
3686 n = (uint8_t) output_length;
3687 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3688 output += n;
3689 output_length -= n;
3690 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003691 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003692 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003693 /* We can't be wanting more output after block 0xff, otherwise
3694 * the capacity check in psa_generator_read() would have
3695 * prevented this call. It could happen only if the generator
3696 * object was corrupted or if this function is called directly
3697 * inside the library. */
3698 if( hkdf->block_number == 0xff )
3699 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003700
3701 /* We need a new block */
3702 ++hkdf->block_number;
3703 hkdf->offset_in_block = 0;
3704 status = psa_hmac_setup_internal( &hkdf->hmac,
3705 hkdf->prk, hash_length,
3706 hash_alg );
3707 if( status != PSA_SUCCESS )
3708 return( status );
3709 if( hkdf->block_number != 1 )
3710 {
3711 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3712 hkdf->output_block,
3713 hash_length );
3714 if( status != PSA_SUCCESS )
3715 return( status );
3716 }
3717 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3718 hkdf->info,
3719 hkdf->info_length );
3720 if( status != PSA_SUCCESS )
3721 return( status );
3722 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3723 &hkdf->block_number, 1 );
3724 if( status != PSA_SUCCESS )
3725 return( status );
3726 status = psa_hmac_finish_internal( &hkdf->hmac,
3727 hkdf->output_block,
3728 sizeof( hkdf->output_block ) );
3729 if( status != PSA_SUCCESS )
3730 return( status );
3731 }
3732
3733 return( PSA_SUCCESS );
3734}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003735
3736static psa_status_t psa_generator_tls12_prf_generate_next_block(
3737 psa_tls12_prf_generator_t *tls12_prf,
3738 psa_algorithm_t alg )
3739{
3740 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3741 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3742 psa_hmac_internal_data hmac;
3743 psa_status_t status, cleanup_status;
3744
Hanno Becker3b339e22018-11-13 20:56:14 +00003745 unsigned char *Ai;
3746 size_t Ai_len;
3747
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003748 /* We can't be wanting more output after block 0xff, otherwise
3749 * the capacity check in psa_generator_read() would have
3750 * prevented this call. It could happen only if the generator
3751 * object was corrupted or if this function is called directly
3752 * inside the library. */
3753 if( tls12_prf->block_number == 0xff )
3754 return( PSA_ERROR_BAD_STATE );
3755
3756 /* We need a new block */
3757 ++tls12_prf->block_number;
3758 tls12_prf->offset_in_block = 0;
3759
3760 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
3761 *
3762 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
3763 *
3764 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
3765 * HMAC_hash(secret, A(2) + seed) +
3766 * HMAC_hash(secret, A(3) + seed) + ...
3767 *
3768 * A(0) = seed
3769 * A(i) = HMAC_hash( secret, A(i-1) )
3770 *
3771 * The `psa_tls12_prf_generator` structures saves the block
3772 * `HMAC_hash(secret, A(i) + seed)` from which the output
3773 * is currently extracted as `output_block`, while
3774 * `A(i) + seed` is stored in `Ai_with_seed`.
3775 *
3776 * Generating a new block means recalculating `Ai_with_seed`
3777 * from the A(i)-part of it, and afterwards recalculating
3778 * `output_block`.
3779 *
3780 * A(0) is computed at setup time.
3781 *
3782 */
3783
3784 psa_hmac_init_internal( &hmac );
3785
3786 /* We must distinguish the calculation of A(1) from those
3787 * of A(2) and higher, because A(0)=seed has a different
3788 * length than the other A(i). */
3789 if( tls12_prf->block_number == 1 )
3790 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003791 Ai = tls12_prf->Ai_with_seed + hash_length;
3792 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003793 }
3794 else
3795 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003796 Ai = tls12_prf->Ai_with_seed;
3797 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003798 }
3799
Hanno Becker3b339e22018-11-13 20:56:14 +00003800 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
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 Ai, Ai_len );
3810 if( status != PSA_SUCCESS )
3811 goto cleanup;
3812
3813 status = psa_hmac_finish_internal( &hmac,
3814 tls12_prf->Ai_with_seed,
3815 hash_length );
3816 if( status != PSA_SUCCESS )
3817 goto cleanup;
3818
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003819 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
3820 status = psa_hmac_setup_internal( &hmac,
3821 tls12_prf->key,
3822 tls12_prf->key_len,
3823 hash_alg );
3824 if( status != PSA_SUCCESS )
3825 goto cleanup;
3826
3827 status = psa_hash_update( &hmac.hash_ctx,
3828 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003829 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003830 if( status != PSA_SUCCESS )
3831 goto cleanup;
3832
3833 status = psa_hmac_finish_internal( &hmac,
3834 tls12_prf->output_block,
3835 hash_length );
3836 if( status != PSA_SUCCESS )
3837 goto cleanup;
3838
3839cleanup:
3840
3841 cleanup_status = psa_hmac_abort_internal( &hmac );
3842 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
3843 status = cleanup_status;
3844
3845 return( status );
3846}
3847
3848/* Read some bytes from an TLS-1.2-PRF-based generator.
3849 * See Section 5 of RFC 5246. */
3850static psa_status_t psa_generator_tls12_prf_read(
3851 psa_tls12_prf_generator_t *tls12_prf,
3852 psa_algorithm_t alg,
3853 uint8_t *output,
3854 size_t output_length )
3855{
3856 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3857 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3858 psa_status_t status;
3859
3860 while( output_length != 0 )
3861 {
3862 /* Copy what remains of the current block */
3863 uint8_t n = hash_length - tls12_prf->offset_in_block;
3864
3865 /* Check if we have fully processed the current block. */
3866 if( n == 0 )
3867 {
3868 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
3869 alg );
3870 if( status != PSA_SUCCESS )
3871 return( status );
3872
3873 continue;
3874 }
3875
3876 if( n > output_length )
3877 n = (uint8_t) output_length;
3878 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
3879 n );
3880 output += n;
3881 output_length -= n;
3882 tls12_prf->offset_in_block += n;
3883 }
3884
3885 return( PSA_SUCCESS );
3886}
Gilles Peskinebef7f142018-07-12 17:22:21 +02003887#endif /* MBEDTLS_MD_C */
3888
Gilles Peskineeab56e42018-07-12 17:12:33 +02003889psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3890 uint8_t *output,
3891 size_t output_length )
3892{
3893 psa_status_t status;
3894
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003895 if( generator->alg == 0 )
3896 {
3897 /* This is a blank generator. */
3898 return PSA_ERROR_BAD_STATE;
3899 }
3900
Gilles Peskineeab56e42018-07-12 17:12:33 +02003901 if( output_length > generator->capacity )
3902 {
3903 generator->capacity = 0;
3904 /* Go through the error path to wipe all confidential data now
3905 * that the generator object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02003906 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003907 goto exit;
3908 }
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003909 if( output_length == 0 && generator->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003910 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003911 /* Edge case: this is a finished generator, and 0 bytes
3912 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02003913 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3914 * INSUFFICIENT_CAPACITY, which is right for a finished
3915 * generator, for consistency with the case when
3916 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02003917 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003918 }
3919 generator->capacity -= output_length;
3920
Gilles Peskine751d9652018-09-18 12:05:44 +02003921 if( generator->alg == PSA_ALG_SELECT_RAW )
3922 {
Gilles Peskine211a4362018-10-25 22:22:31 +02003923 /* Initially, the capacity of a selection generator is always
3924 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
3925 * abbreviated in this comment as `size`. When the remaining
3926 * capacity is `c`, the next bytes to serve start `c` bytes
3927 * from the end of the buffer, i.e. `size - c` from the
3928 * beginning of the buffer. Since `generator->capacity` was just
3929 * decremented above, we need to serve the bytes from
3930 * `size - generator->capacity - output_length` to
3931 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02003932 size_t offset =
3933 generator->ctx.buffer.size - generator->capacity - output_length;
3934 memcpy( output, generator->ctx.buffer.data + offset, output_length );
3935 status = PSA_SUCCESS;
3936 }
3937 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003938#if defined(MBEDTLS_MD_C)
3939 if( PSA_ALG_IS_HKDF( generator->alg ) )
3940 {
3941 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3942 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3943 output, output_length );
3944 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003945 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3946 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003947 {
3948 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
3949 generator->alg, output,
3950 output_length );
3951 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003952 else
3953#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003954 {
3955 return( PSA_ERROR_BAD_STATE );
3956 }
3957
3958exit:
3959 if( status != PSA_SUCCESS )
3960 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003961 /* Preserve the algorithm upon errors, but clear all sensitive state.
3962 * This allows us to differentiate between exhausted generators and
3963 * blank generators, so we can return PSA_ERROR_BAD_STATE on blank
3964 * generators. */
3965 psa_algorithm_t alg = generator->alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003966 psa_generator_abort( generator );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003967 generator->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003968 memset( output, '!', output_length );
3969 }
3970 return( status );
3971}
3972
Gilles Peskine08542d82018-07-19 17:05:42 +02003973#if defined(MBEDTLS_DES_C)
3974static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3975{
3976 if( data_size >= 8 )
3977 mbedtls_des_key_set_parity( data );
3978 if( data_size >= 16 )
3979 mbedtls_des_key_set_parity( data + 8 );
3980 if( data_size >= 24 )
3981 mbedtls_des_key_set_parity( data + 16 );
3982}
3983#endif /* MBEDTLS_DES_C */
3984
Gilles Peskinec5487a82018-12-03 18:08:14 +01003985psa_status_t psa_generator_import_key( psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003986 psa_key_type_t type,
3987 size_t bits,
3988 psa_crypto_generator_t *generator )
3989{
3990 uint8_t *data = NULL;
3991 size_t bytes = PSA_BITS_TO_BYTES( bits );
3992 psa_status_t status;
3993
3994 if( ! key_type_is_raw_bytes( type ) )
3995 return( PSA_ERROR_INVALID_ARGUMENT );
3996 if( bits % 8 != 0 )
3997 return( PSA_ERROR_INVALID_ARGUMENT );
3998 data = mbedtls_calloc( 1, bytes );
3999 if( data == NULL )
4000 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4001
4002 status = psa_generator_read( generator, data, bytes );
4003 if( status != PSA_SUCCESS )
4004 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004005#if defined(MBEDTLS_DES_C)
4006 if( type == PSA_KEY_TYPE_DES )
4007 psa_des_set_key_parity( data, bytes );
4008#endif /* MBEDTLS_DES_C */
Gilles Peskinec5487a82018-12-03 18:08:14 +01004009 status = psa_import_key( handle, type, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004010
4011exit:
4012 mbedtls_free( data );
4013 return( status );
4014}
4015
4016
4017
4018/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004019/* Key derivation */
4020/****************************************************************/
4021
Gilles Peskinea05219c2018-11-16 16:02:56 +01004022#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02004023/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004024 * of the HKDF algorithm.
4025 *
4026 * Note that if this function fails, you must call psa_generator_abort()
4027 * to potentially free embedded data structures and wipe confidential data.
4028 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004029static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004030 const uint8_t *secret,
4031 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004032 psa_algorithm_t hash_alg,
4033 const uint8_t *salt,
4034 size_t salt_length,
4035 const uint8_t *label,
4036 size_t label_length )
4037{
4038 psa_status_t status;
4039 status = psa_hmac_setup_internal( &hkdf->hmac,
4040 salt, salt_length,
Gilles Peskine00709fa2018-08-22 18:25:41 +02004041 PSA_ALG_HMAC_GET_HASH( hash_alg ) );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004042 if( status != PSA_SUCCESS )
4043 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004044 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004045 if( status != PSA_SUCCESS )
4046 return( status );
4047 status = psa_hmac_finish_internal( &hkdf->hmac,
4048 hkdf->prk,
4049 sizeof( hkdf->prk ) );
4050 if( status != PSA_SUCCESS )
4051 return( status );
4052 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4053 hkdf->block_number = 0;
4054 hkdf->info_length = label_length;
4055 if( label_length != 0 )
4056 {
4057 hkdf->info = mbedtls_calloc( 1, label_length );
4058 if( hkdf->info == NULL )
4059 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4060 memcpy( hkdf->info, label, label_length );
4061 }
4062 return( PSA_SUCCESS );
4063}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004064#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004065
Gilles Peskinea05219c2018-11-16 16:02:56 +01004066#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01004067/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
4068 *
4069 * Note that if this function fails, you must call psa_generator_abort()
4070 * to potentially free embedded data structures and wipe confidential data.
4071 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004072static psa_status_t psa_generator_tls12_prf_setup(
4073 psa_tls12_prf_generator_t *tls12_prf,
4074 const unsigned char *key,
4075 size_t key_len,
4076 psa_algorithm_t hash_alg,
4077 const uint8_t *salt,
4078 size_t salt_length,
4079 const uint8_t *label,
4080 size_t label_length )
4081{
4082 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004083 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4084 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004085
4086 tls12_prf->key = mbedtls_calloc( 1, key_len );
4087 if( tls12_prf->key == NULL )
4088 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4089 tls12_prf->key_len = key_len;
4090 memcpy( tls12_prf->key, key, key_len );
4091
Hanno Becker580fba12018-11-13 20:50:45 +00004092 overflow = ( salt_length + label_length < salt_length ) ||
4093 ( salt_length + label_length + hash_length < hash_length );
4094 if( overflow )
4095 return( PSA_ERROR_INVALID_ARGUMENT );
4096
4097 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4098 if( tls12_prf->Ai_with_seed == NULL )
4099 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4100 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4101
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004102 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4103 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004104 if( label_length != 0 )
4105 {
4106 memcpy( tls12_prf->Ai_with_seed + hash_length,
4107 label, label_length );
4108 }
4109
4110 if( salt_length != 0 )
4111 {
4112 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4113 salt, salt_length );
4114 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004115
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004116 /* The first block gets generated when
4117 * psa_generator_read() is called. */
4118 tls12_prf->block_number = 0;
4119 tls12_prf->offset_in_block = hash_length;
4120
4121 return( PSA_SUCCESS );
4122}
Hanno Becker1aaedc02018-11-16 11:35:34 +00004123
4124/* Set up a TLS-1.2-PSK-to-MS-based generator. */
4125static psa_status_t psa_generator_tls12_psk_to_ms_setup(
4126 psa_tls12_prf_generator_t *tls12_prf,
4127 const unsigned char *psk,
4128 size_t psk_len,
4129 psa_algorithm_t hash_alg,
4130 const uint8_t *salt,
4131 size_t salt_length,
4132 const uint8_t *label,
4133 size_t label_length )
4134{
4135 psa_status_t status;
4136 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4137
4138 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4139 return( PSA_ERROR_INVALID_ARGUMENT );
4140
4141 /* Quoting RFC 4279, Section 2:
4142 *
4143 * The premaster secret is formed as follows: if the PSK is N octets
4144 * long, concatenate a uint16 with the value N, N zero octets, a second
4145 * uint16 with the value N, and the PSK itself.
4146 */
4147
4148 pms[0] = ( psk_len >> 8 ) & 0xff;
4149 pms[1] = ( psk_len >> 0 ) & 0xff;
4150 memset( pms + 2, 0, psk_len );
4151 pms[2 + psk_len + 0] = pms[0];
4152 pms[2 + psk_len + 1] = pms[1];
4153 memcpy( pms + 4 + psk_len, psk, psk_len );
4154
4155 status = psa_generator_tls12_prf_setup( tls12_prf,
4156 pms, 4 + 2 * psk_len,
4157 hash_alg,
4158 salt, salt_length,
4159 label, label_length );
4160
Gilles Peskine3f108122018-12-07 18:14:53 +01004161 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004162 return( status );
4163}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004164#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004165
Gilles Peskine346797d2018-11-16 16:05:06 +01004166/* Note that if this function fails, you must call psa_generator_abort()
4167 * to potentially free embedded data structures and wipe confidential data.
4168 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004169static psa_status_t psa_key_derivation_internal(
4170 psa_crypto_generator_t *generator,
4171 const uint8_t *secret, size_t secret_length,
4172 psa_algorithm_t alg,
4173 const uint8_t *salt, size_t salt_length,
4174 const uint8_t *label, size_t label_length,
4175 size_t capacity )
4176{
4177 psa_status_t status;
4178 size_t max_capacity;
4179
4180 /* Set generator->alg even on failure so that abort knows what to do. */
4181 generator->alg = alg;
4182
Gilles Peskine751d9652018-09-18 12:05:44 +02004183 if( alg == PSA_ALG_SELECT_RAW )
4184 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01004185 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02004186 if( salt_length != 0 )
4187 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01004188 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02004189 if( label_length != 0 )
4190 return( PSA_ERROR_INVALID_ARGUMENT );
4191 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
4192 if( generator->ctx.buffer.data == NULL )
4193 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4194 memcpy( generator->ctx.buffer.data, secret, secret_length );
4195 generator->ctx.buffer.size = secret_length;
4196 max_capacity = secret_length;
4197 status = PSA_SUCCESS;
4198 }
4199 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004200#if defined(MBEDTLS_MD_C)
4201 if( PSA_ALG_IS_HKDF( alg ) )
4202 {
4203 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4204 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4205 if( hash_size == 0 )
4206 return( PSA_ERROR_NOT_SUPPORTED );
4207 max_capacity = 255 * hash_size;
4208 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
4209 secret, secret_length,
4210 hash_alg,
4211 salt, salt_length,
4212 label, label_length );
4213 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004214 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4215 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4216 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004217 {
4218 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4219 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4220
4221 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4222 if( hash_alg != PSA_ALG_SHA_256 &&
4223 hash_alg != PSA_ALG_SHA_384 )
4224 {
4225 return( PSA_ERROR_NOT_SUPPORTED );
4226 }
4227
4228 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004229
4230 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4231 {
4232 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
4233 secret, secret_length,
4234 hash_alg, salt, salt_length,
4235 label, label_length );
4236 }
4237 else
4238 {
4239 status = psa_generator_tls12_psk_to_ms_setup(
4240 &generator->ctx.tls12_prf,
4241 secret, secret_length,
4242 hash_alg, salt, salt_length,
4243 label, label_length );
4244 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004245 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004246 else
4247#endif
4248 {
4249 return( PSA_ERROR_NOT_SUPPORTED );
4250 }
4251
4252 if( status != PSA_SUCCESS )
4253 return( status );
4254
4255 if( capacity <= max_capacity )
4256 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02004257 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
4258 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004259 else
4260 return( PSA_ERROR_INVALID_ARGUMENT );
4261
4262 return( PSA_SUCCESS );
4263}
4264
Gilles Peskineea0fb492018-07-12 17:17:20 +02004265psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004266 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004267 psa_algorithm_t alg,
4268 const uint8_t *salt,
4269 size_t salt_length,
4270 const uint8_t *label,
4271 size_t label_length,
4272 size_t capacity )
4273{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004274 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004275 psa_status_t status;
4276
4277 if( generator->alg != 0 )
4278 return( PSA_ERROR_BAD_STATE );
4279
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004280 /* Make sure that alg is a key derivation algorithm. This prevents
4281 * key selection algorithms, which psa_key_derivation_internal
4282 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004283 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4284 return( PSA_ERROR_INVALID_ARGUMENT );
4285
Gilles Peskinec5487a82018-12-03 18:08:14 +01004286 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004287 if( status != PSA_SUCCESS )
4288 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004289
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004290 if( slot->type != PSA_KEY_TYPE_DERIVE )
4291 return( PSA_ERROR_INVALID_ARGUMENT );
4292
4293 status = psa_key_derivation_internal( generator,
4294 slot->data.raw.data,
4295 slot->data.raw.bytes,
4296 alg,
4297 salt, salt_length,
4298 label, label_length,
4299 capacity );
4300 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004301 psa_generator_abort( generator );
4302 return( status );
4303}
4304
4305
4306
4307/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004308/* Key agreement */
4309/****************************************************************/
4310
Gilles Peskinea05219c2018-11-16 16:02:56 +01004311#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004312static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4313 size_t peer_key_length,
4314 const mbedtls_ecp_keypair *our_key,
4315 uint8_t *shared_secret,
4316 size_t shared_secret_size,
4317 size_t *shared_secret_length )
4318{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004319 mbedtls_ecp_keypair *their_key = NULL;
4320 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00004321 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004322 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004323
Jaeden Ameroccdce902019-01-10 11:42:27 +00004324 status = psa_import_ec_public_key(
4325 mbedtls_ecc_group_to_psa( our_key->grp.id ),
4326 peer_key, peer_key_length,
4327 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00004328 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004329 goto exit;
4330
Jaeden Amero97271b32019-01-10 19:38:51 +00004331 status = mbedtls_to_psa_error(
4332 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
4333 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004334 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00004335 status = mbedtls_to_psa_error(
4336 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
4337 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004338 goto exit;
4339
Jaeden Amero97271b32019-01-10 19:38:51 +00004340 status = mbedtls_to_psa_error(
4341 mbedtls_ecdh_calc_secret( &ecdh,
4342 shared_secret_length,
4343 shared_secret, shared_secret_size,
4344 mbedtls_ctr_drbg_random,
4345 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004346
4347exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004348 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00004349 mbedtls_ecp_keypair_free( their_key );
4350 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00004351 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004352}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004353#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004354
Gilles Peskine01d718c2018-09-18 12:01:02 +02004355#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4356
Gilles Peskine346797d2018-11-16 16:05:06 +01004357/* Note that if this function fails, you must call psa_generator_abort()
4358 * to potentially free embedded data structures and wipe confidential data.
4359 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004360static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
Gilles Peskine2f060a82018-12-04 17:12:32 +01004361 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004362 const uint8_t *peer_key,
4363 size_t peer_key_length,
4364 psa_algorithm_t alg )
4365{
4366 psa_status_t status;
4367 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4368 size_t shared_secret_length = 0;
4369
4370 /* Step 1: run the secret agreement algorithm to generate the shared
4371 * secret. */
4372 switch( PSA_ALG_KEY_AGREEMENT_GET_BASE( alg ) )
4373 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004374#if defined(MBEDTLS_ECDH_C)
4375 case PSA_ALG_ECDH_BASE:
4376 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
4377 return( PSA_ERROR_INVALID_ARGUMENT );
4378 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
4379 private_key->data.ecp,
4380 shared_secret,
4381 sizeof( shared_secret ),
4382 &shared_secret_length );
4383 break;
4384#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004385 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01004386 (void) private_key;
4387 (void) peer_key;
4388 (void) peer_key_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004389 return( PSA_ERROR_NOT_SUPPORTED );
4390 }
4391 if( status != PSA_SUCCESS )
4392 goto exit;
4393
4394 /* Step 2: set up the key derivation to generate key material from
4395 * the shared secret. */
4396 status = psa_key_derivation_internal( generator,
4397 shared_secret, shared_secret_length,
4398 PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ),
4399 NULL, 0, NULL, 0,
4400 PSA_GENERATOR_UNBRIDLED_CAPACITY );
4401exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01004402 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004403 return( status );
4404}
4405
4406psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004407 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004408 const uint8_t *peer_key,
4409 size_t peer_key_length,
4410 psa_algorithm_t alg )
4411{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004412 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004413 psa_status_t status;
4414 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
4415 return( PSA_ERROR_INVALID_ARGUMENT );
4416 status = psa_get_key_from_slot( private_key, &slot,
4417 PSA_KEY_USAGE_DERIVE, alg );
4418 if( status != PSA_SUCCESS )
4419 return( status );
Gilles Peskine346797d2018-11-16 16:05:06 +01004420 status = psa_key_agreement_internal( generator,
4421 slot,
4422 peer_key, peer_key_length,
4423 alg );
4424 if( status != PSA_SUCCESS )
4425 psa_generator_abort( generator );
4426 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004427}
4428
4429
4430
4431/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004432/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02004433/****************************************************************/
4434
4435psa_status_t psa_generate_random( uint8_t *output,
4436 size_t output_size )
4437{
itayzafrir0adf0fc2018-09-06 16:24:41 +03004438 int ret;
4439 GUARD_MODULE_INITIALIZED;
4440
4441 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02004442 return( mbedtls_to_psa_error( ret ) );
4443}
4444
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01004445#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
4446#include "mbedtls/entropy_poll.h"
4447
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004448psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
4449 size_t seed_size )
4450{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004451 if( global_data.initialized )
4452 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02004453
4454 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
4455 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
4456 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
4457 return( PSA_ERROR_INVALID_ARGUMENT );
4458
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01004459 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004460}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01004461#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004462
Gilles Peskinec5487a82018-12-03 18:08:14 +01004463psa_status_t psa_generate_key( psa_key_handle_t handle,
Gilles Peskine05d69892018-06-19 22:00:52 +02004464 psa_key_type_t type,
4465 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02004466 const void *extra,
4467 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02004468{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004469 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004470 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004471
Gilles Peskine53d991e2018-07-12 01:14:59 +02004472 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004473 return( PSA_ERROR_INVALID_ARGUMENT );
4474
Gilles Peskinec5487a82018-12-03 18:08:14 +01004475 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004476 if( status != PSA_SUCCESS )
4477 return( status );
4478
Gilles Peskine48c0ea12018-06-21 14:15:31 +02004479 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004480 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004481 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004482 if( status != PSA_SUCCESS )
4483 return( status );
4484 status = psa_generate_random( slot->data.raw.data,
4485 slot->data.raw.bytes );
4486 if( status != PSA_SUCCESS )
4487 {
4488 mbedtls_free( slot->data.raw.data );
4489 return( status );
4490 }
4491#if defined(MBEDTLS_DES_C)
4492 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004493 psa_des_set_key_parity( slot->data.raw.data,
4494 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004495#endif /* MBEDTLS_DES_C */
4496 }
4497 else
4498
4499#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
4500 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
4501 {
4502 mbedtls_rsa_context *rsa;
4503 int ret;
4504 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02004505 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
4506 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01004507 /* Accept only byte-aligned keys, for the same reasons as
4508 * in psa_import_rsa_key(). */
4509 if( bits % 8 != 0 )
4510 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02004511 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004512 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02004513 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004514 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004515 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02004516#if INT_MAX < 0xffffffff
4517 /* Check that the uint32_t value passed by the caller fits
4518 * in the range supported by this implementation. */
4519 if( p->e > INT_MAX )
4520 return( PSA_ERROR_NOT_SUPPORTED );
4521#endif
4522 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004523 }
4524 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
4525 if( rsa == NULL )
4526 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4527 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
4528 ret = mbedtls_rsa_gen_key( rsa,
4529 mbedtls_ctr_drbg_random,
4530 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004531 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02004532 exponent );
4533 if( ret != 0 )
4534 {
4535 mbedtls_rsa_free( rsa );
4536 mbedtls_free( rsa );
4537 return( mbedtls_to_psa_error( ret ) );
4538 }
4539 slot->data.rsa = rsa;
4540 }
4541 else
4542#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
4543
4544#if defined(MBEDTLS_ECP_C)
4545 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
4546 {
4547 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
4548 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
4549 const mbedtls_ecp_curve_info *curve_info =
4550 mbedtls_ecp_curve_info_from_grp_id( grp_id );
4551 mbedtls_ecp_keypair *ecp;
4552 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004553 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004554 return( PSA_ERROR_NOT_SUPPORTED );
4555 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
4556 return( PSA_ERROR_NOT_SUPPORTED );
4557 if( curve_info->bit_size != bits )
4558 return( PSA_ERROR_INVALID_ARGUMENT );
4559 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
4560 if( ecp == NULL )
4561 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4562 mbedtls_ecp_keypair_init( ecp );
4563 ret = mbedtls_ecp_gen_key( grp_id, ecp,
4564 mbedtls_ctr_drbg_random,
4565 &global_data.ctr_drbg );
4566 if( ret != 0 )
4567 {
4568 mbedtls_ecp_keypair_free( ecp );
4569 mbedtls_free( ecp );
4570 return( mbedtls_to_psa_error( ret ) );
4571 }
4572 slot->data.ecp = ecp;
4573 }
4574 else
4575#endif /* MBEDTLS_ECP_C */
4576
4577 return( PSA_ERROR_NOT_SUPPORTED );
4578
4579 slot->type = type;
Darryl Green0c6575a2018-11-07 16:05:30 +00004580
4581#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
4582 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
4583 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01004584 return( psa_save_generated_persistent_key( slot, bits ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004585 }
4586#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
4587
4588 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02004589}
4590
4591
4592/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01004593/* Module setup */
4594/****************************************************************/
4595
Gilles Peskine5e769522018-11-20 21:59:56 +01004596psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
4597 void (* entropy_init )( mbedtls_entropy_context *ctx ),
4598 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
4599{
4600 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4601 return( PSA_ERROR_BAD_STATE );
4602 global_data.entropy_init = entropy_init;
4603 global_data.entropy_free = entropy_free;
4604 return( PSA_SUCCESS );
4605}
4606
Gilles Peskinee59236f2018-01-27 23:32:46 +01004607void mbedtls_psa_crypto_free( void )
4608{
Gilles Peskine66fb1262018-12-10 16:29:04 +01004609 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004610 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4611 {
4612 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01004613 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004614 }
4615 /* Wipe all remaining data, including configuration.
4616 * In particular, this sets all state indicator to the value
4617 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01004618 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004619}
4620
4621psa_status_t psa_crypto_init( void )
4622{
Gilles Peskine66fb1262018-12-10 16:29:04 +01004623 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004624 const unsigned char drbg_seed[] = "PSA";
4625
Gilles Peskinec6b69072018-11-20 21:42:52 +01004626 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004627 if( global_data.initialized != 0 )
4628 return( PSA_SUCCESS );
4629
Gilles Peskine5e769522018-11-20 21:59:56 +01004630 /* Set default configuration if
4631 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
4632 if( global_data.entropy_init == NULL )
4633 global_data.entropy_init = mbedtls_entropy_init;
4634 if( global_data.entropy_free == NULL )
4635 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004636
Gilles Peskinec6b69072018-11-20 21:42:52 +01004637 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01004638 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004639 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004640 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01004641 status = mbedtls_to_psa_error(
4642 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
4643 mbedtls_entropy_func,
4644 &global_data.entropy,
4645 drbg_seed, sizeof( drbg_seed ) - 1 ) );
4646 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004647 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004648 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004649
Gilles Peskine66fb1262018-12-10 16:29:04 +01004650 status = psa_initialize_key_slots( );
4651 if( status != PSA_SUCCESS )
4652 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004653
4654 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01004655 global_data.initialized = 1;
4656
Gilles Peskinee59236f2018-01-27 23:32:46 +01004657exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01004658 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004659 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01004660 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004661}
4662
4663#endif /* MBEDTLS_PSA_CRYPTO_C */