blob: 3c318727fd85ebf0ac4a2b3638b8767d1a4d7647 [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
Gilles Peskine2c86ebc2019-05-13 14:21:57 +0200624 if( PSA_BITS_TO_BYTES( PSA_ECC_CURVE_BITS( curve ) ) != data_length )
625 return( PSA_ERROR_INVALID_ARGUMENT );
626
Gilles Peskinef76aa772018-10-29 19:24:33 +0100627 *p_ecp = NULL;
628 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
629 if( ecp == NULL )
630 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000631 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100632
633 /* Load the group. */
634 status = mbedtls_to_psa_error(
635 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
636 if( status != PSA_SUCCESS )
637 goto exit;
638 /* Load the secret value. */
639 status = mbedtls_to_psa_error(
640 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
641 if( status != PSA_SUCCESS )
642 goto exit;
643 /* Validate the private key. */
644 status = mbedtls_to_psa_error(
645 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
646 if( status != PSA_SUCCESS )
647 goto exit;
648 /* Calculate the public key from the private key. */
649 status = mbedtls_to_psa_error(
650 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
651 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
652 if( status != PSA_SUCCESS )
653 goto exit;
654
655 *p_ecp = ecp;
656 return( PSA_SUCCESS );
657
658exit:
659 if( ecp != NULL )
660 {
661 mbedtls_ecp_keypair_free( ecp );
662 mbedtls_free( ecp );
663 }
664 return( status );
665}
666#endif /* defined(MBEDTLS_ECP_C) */
667
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100668/** Import key data into a slot. `slot->type` must have been set
669 * previously. This function assumes that the slot does not contain
670 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100671psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
672 const uint8_t *data,
673 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100674{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200675 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100676
Darryl Green940d72c2018-07-13 13:18:51 +0100677 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100678 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100679 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100680 if( data_length > SIZE_MAX / 8 )
681 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green940d72c2018-07-13 13:18:51 +0100682 status = prepare_raw_data_slot( slot->type,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200683 PSA_BYTES_TO_BITS( data_length ),
684 &slot->data.raw );
685 if( status != PSA_SUCCESS )
686 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200687 if( data_length != 0 )
688 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100689 }
690 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100691#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100692 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100693 {
Darryl Green940d72c2018-07-13 13:18:51 +0100694 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100695 data, data_length,
696 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100697 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000698 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100699 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000700 status = psa_import_ec_public_key(
701 PSA_KEY_TYPE_GET_CURVE( slot->type ),
702 data, data_length,
703 &slot->data.ecp );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100704 }
705 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100706#endif /* MBEDTLS_ECP_C */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000707#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
708 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100709 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000710 status = psa_import_rsa_key( slot->type,
711 data, data_length,
712 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100713 }
714 else
Jaeden Ameroccdce902019-01-10 11:42:27 +0000715#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100716 {
717 return( PSA_ERROR_NOT_SUPPORTED );
718 }
Jaeden Ameroc67200d2019-01-14 13:12:39 +0000719 return( status );
Darryl Green940d72c2018-07-13 13:18:51 +0100720}
721
Darryl Green06fd18d2018-07-16 11:21:11 +0100722/* Retrieve an empty key slot (slot with no key data, but possibly
723 * with some metadata such as a policy). */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100724static psa_status_t psa_get_empty_key_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100725 psa_key_slot_t **p_slot )
Darryl Green06fd18d2018-07-16 11:21:11 +0100726{
727 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100728 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100729
730 *p_slot = NULL;
731
Gilles Peskinec5487a82018-12-03 18:08:14 +0100732 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100733 if( status != PSA_SUCCESS )
734 return( status );
735
736 if( slot->type != PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200737 return( PSA_ERROR_ALREADY_EXISTS );
Darryl Green06fd18d2018-07-16 11:21:11 +0100738
739 *p_slot = slot;
740 return( status );
741}
742
Gilles Peskinef603c712019-01-19 13:40:11 +0100743/** Calculate the intersection of two algorithm usage policies.
744 *
745 * Return 0 (which allows no operation) on incompatibility.
746 */
747static psa_algorithm_t psa_key_policy_algorithm_intersection(
748 psa_algorithm_t alg1,
749 psa_algorithm_t alg2 )
750{
Gilles Peskine549ea862019-05-22 11:45:59 +0200751 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100752 if( alg1 == alg2 )
753 return( alg1 );
754 /* If the policies are from the same hash-and-sign family, check
755 * if one is a wildcard. If so the other has the specific algorithm. */
756 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
757 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
758 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
759 {
760 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
761 return( alg2 );
762 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
763 return( alg1 );
764 }
765 /* If the policies are incompatible, allow nothing. */
766 return( 0 );
767}
768
Gilles Peskined6f371b2019-05-10 19:33:38 +0200769static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
770 psa_algorithm_t requested_alg )
771{
Gilles Peskine549ea862019-05-22 11:45:59 +0200772 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200773 if( requested_alg == policy_alg )
774 return( 1 );
775 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +0200776 * and requested_alg is the same hash-and-sign family with any hash,
777 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200778 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
779 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
780 {
781 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
782 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
783 }
784 /* If it isn't permitted, it's forbidden. */
785 return( 0 );
786}
787
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100788/** Test whether a policy permits an algorithm.
789 *
790 * The caller must test usage flags separately.
791 */
792static int psa_key_policy_permits( const psa_key_policy_t *policy,
793 psa_algorithm_t alg )
794{
Gilles Peskined6f371b2019-05-10 19:33:38 +0200795 return( psa_key_algorithm_permits( policy->alg, alg ) ||
796 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100797}
798
Gilles Peskinef603c712019-01-19 13:40:11 +0100799/** Restrict a key policy based on a constraint.
800 *
801 * \param[in,out] policy The policy to restrict.
802 * \param[in] constraint The policy constraint to apply.
803 *
804 * \retval #PSA_SUCCESS
805 * \c *policy contains the intersection of the original value of
806 * \c *policy and \c *constraint.
807 * \retval #PSA_ERROR_INVALID_ARGUMENT
808 * \c *policy and \c *constraint are incompatible.
809 * \c *policy is unchanged.
810 */
811static psa_status_t psa_restrict_key_policy(
812 psa_key_policy_t *policy,
813 const psa_key_policy_t *constraint )
814{
815 psa_algorithm_t intersection_alg =
816 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200817 psa_algorithm_t intersection_alg2 =
818 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100819 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
820 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200821 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
822 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100823 policy->usage &= constraint->usage;
824 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200825 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100826 return( PSA_SUCCESS );
827}
828
Darryl Green06fd18d2018-07-16 11:21:11 +0100829/** Retrieve a slot which must contain a key. The key must have allow all the
830 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
831 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100832static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100833 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100834 psa_key_usage_t usage,
835 psa_algorithm_t alg )
836{
837 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100838 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100839
840 *p_slot = NULL;
841
Gilles Peskinec5487a82018-12-03 18:08:14 +0100842 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100843 if( status != PSA_SUCCESS )
844 return( status );
845 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200846 return( PSA_ERROR_DOES_NOT_EXIST );
Darryl Green06fd18d2018-07-16 11:21:11 +0100847
848 /* Enforce that usage policy for the key slot contains all the flags
849 * required by the usage parameter. There is one exception: public
850 * keys can always be exported, so we treat public key objects as
851 * if they had the export flag. */
852 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
853 usage &= ~PSA_KEY_USAGE_EXPORT;
854 if( ( slot->policy.usage & usage ) != usage )
855 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100856
857 /* Enforce that the usage policy permits the requested algortihm. */
858 if( alg != 0 && ! psa_key_policy_permits( &slot->policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100859 return( PSA_ERROR_NOT_PERMITTED );
860
861 *p_slot = slot;
862 return( PSA_SUCCESS );
863}
Darryl Green940d72c2018-07-13 13:18:51 +0100864
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100865/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100866static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000867{
868 if( slot->type == PSA_KEY_TYPE_NONE )
869 {
870 /* No key material to clean. */
871 }
872 else if( key_type_is_raw_bytes( slot->type ) )
873 {
874 mbedtls_free( slot->data.raw.data );
875 }
876 else
877#if defined(MBEDTLS_RSA_C)
878 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
879 {
880 mbedtls_rsa_free( slot->data.rsa );
881 mbedtls_free( slot->data.rsa );
882 }
883 else
884#endif /* defined(MBEDTLS_RSA_C) */
885#if defined(MBEDTLS_ECP_C)
886 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
887 {
888 mbedtls_ecp_keypair_free( slot->data.ecp );
889 mbedtls_free( slot->data.ecp );
890 }
891 else
892#endif /* defined(MBEDTLS_ECP_C) */
893 {
894 /* Shouldn't happen: the key type is not any type that we
895 * put in. */
896 return( PSA_ERROR_TAMPERING_DETECTED );
897 }
898
899 return( PSA_SUCCESS );
900}
901
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100902/** Completely wipe a slot in memory, including its policy.
903 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100904psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100905{
906 psa_status_t status = psa_remove_key_data_from_memory( slot );
907 /* At this point, key material and other type-specific content has
908 * been wiped. Clear remaining metadata. We can call memset and not
909 * zeroize because the metadata is not particularly sensitive. */
910 memset( slot, 0, sizeof( *slot ) );
911 return( status );
912}
913
Gilles Peskinec5487a82018-12-03 18:08:14 +0100914psa_status_t psa_import_key( psa_key_handle_t handle,
Darryl Green940d72c2018-07-13 13:18:51 +0100915 psa_key_type_t type,
916 const uint8_t *data,
917 size_t data_length )
918{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100919 psa_key_slot_t *slot;
Darryl Green940d72c2018-07-13 13:18:51 +0100920 psa_status_t status;
921
Gilles Peskinec5487a82018-12-03 18:08:14 +0100922 status = psa_get_empty_key_slot( handle, &slot );
Darryl Green940d72c2018-07-13 13:18:51 +0100923 if( status != PSA_SUCCESS )
924 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100925
926 slot->type = type;
Darryl Green940d72c2018-07-13 13:18:51 +0100927
928 status = psa_import_key_into_slot( slot, data, data_length );
929 if( status != PSA_SUCCESS )
930 {
931 slot->type = PSA_KEY_TYPE_NONE;
932 return( status );
933 }
934
Darryl Greend49a4992018-06-18 17:27:26 +0100935#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
936 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
937 {
938 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +0100939 status = psa_save_persistent_key( slot->persistent_storage_id,
940 slot->type, &slot->policy, data,
Darryl Greend49a4992018-06-18 17:27:26 +0100941 data_length );
942 if( status != PSA_SUCCESS )
943 {
944 (void) psa_remove_key_data_from_memory( slot );
945 slot->type = PSA_KEY_TYPE_NONE;
946 }
947 }
948#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
949
950 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100951}
952
Gilles Peskinec5487a82018-12-03 18:08:14 +0100953psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100954{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100955 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100956 psa_status_t status = PSA_SUCCESS;
957 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100958
Gilles Peskinec5487a82018-12-03 18:08:14 +0100959 status = psa_get_key_slot( handle, &slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100960 if( status != PSA_SUCCESS )
961 return( status );
Darryl Greend49a4992018-06-18 17:27:26 +0100962#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
963 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
964 {
Gilles Peskine69f976b2018-11-30 18:46:56 +0100965 storage_status =
966 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +0100967 }
968#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100969 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +0100970 if( status != PSA_SUCCESS )
971 return( status );
972 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100973}
974
Gilles Peskineb870b182018-07-06 16:02:09 +0200975/* Return the size of the key in the given slot, in bits. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100976static size_t psa_get_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +0200977{
978 if( key_type_is_raw_bytes( slot->type ) )
979 return( slot->data.raw.bytes * 8 );
980#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200981 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +0100982 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +0200983#endif /* defined(MBEDTLS_RSA_C) */
984#if defined(MBEDTLS_ECP_C)
985 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
986 return( slot->data.ecp->grp.pbits );
987#endif /* defined(MBEDTLS_ECP_C) */
988 /* Shouldn't happen except on an empty slot. */
989 return( 0 );
990}
991
Gilles Peskinec5487a82018-12-03 18:08:14 +0100992psa_status_t psa_get_key_information( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +0200993 psa_key_type_t *type,
994 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100995{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100996 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200997 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100998
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100999 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001000 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001001 if( bits != NULL )
1002 *bits = 0;
Gilles Peskinec5487a82018-12-03 18:08:14 +01001003 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001004 if( status != PSA_SUCCESS )
1005 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001006
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001007 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +02001008 return( PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001009 if( type != NULL )
1010 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +02001011 if( bits != NULL )
1012 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001013 return( PSA_SUCCESS );
1014}
1015
Jaeden Ameroccdce902019-01-10 11:42:27 +00001016#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero25384a22019-01-10 10:23:21 +00001017static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1018 unsigned char *buf, size_t size )
1019{
1020 int ret;
1021 unsigned char *c;
1022 size_t len = 0;
1023
1024 c = buf + size;
1025
1026 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1027
1028 return( (int) len );
1029}
Jaeden Ameroccdce902019-01-10 11:42:27 +00001030#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero25384a22019-01-10 10:23:21 +00001031
Gilles Peskinef603c712019-01-19 13:40:11 +01001032static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1033 uint8_t *data,
1034 size_t data_size,
1035 size_t *data_length,
1036 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001037{
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001038 *data_length = 0;
1039
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001040 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001041 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001042
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001043 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001044 {
1045 if( slot->data.raw.bytes > data_size )
1046 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001047 if( data_size != 0 )
1048 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001049 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001050 memset( data + slot->data.raw.bytes, 0,
1051 data_size - slot->data.raw.bytes );
1052 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001053 *data_length = slot->data.raw.bytes;
1054 return( PSA_SUCCESS );
1055 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001056#if defined(MBEDTLS_ECP_C)
1057 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
1058 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001059 psa_status_t status;
1060
Gilles Peskine188c71e2018-10-29 19:26:02 +01001061 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( slot ) );
1062 if( bytes > data_size )
1063 return( PSA_ERROR_BUFFER_TOO_SMALL );
1064 status = mbedtls_to_psa_error(
1065 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1066 if( status != PSA_SUCCESS )
1067 return( status );
1068 memset( data + bytes, 0, data_size - bytes );
1069 *data_length = bytes;
1070 return( PSA_SUCCESS );
1071 }
1072#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001073 else
Moran Peker17e36e12018-05-02 12:55:20 +03001074 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001075#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001076 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001077 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001078 {
Moran Pekera998bc62018-04-16 18:16:20 +03001079 mbedtls_pk_context pk;
1080 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001081 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001082 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001083#if defined(MBEDTLS_RSA_C)
1084 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001085 pk.pk_info = &mbedtls_rsa_info;
1086 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001087#else
1088 return( PSA_ERROR_NOT_SUPPORTED );
1089#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001090 }
1091 else
1092 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001093#if defined(MBEDTLS_ECP_C)
1094 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001095 pk.pk_info = &mbedtls_eckey_info;
1096 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001097#else
1098 return( PSA_ERROR_NOT_SUPPORTED );
1099#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001100 }
Moran Pekerd7326592018-05-29 16:56:39 +03001101 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Jaeden Amero25384a22019-01-10 10:23:21 +00001102 {
Jaeden Ameroccdce902019-01-10 11:42:27 +00001103 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001104 }
Moran Peker17e36e12018-05-02 12:55:20 +03001105 else
Jaeden Amero25384a22019-01-10 10:23:21 +00001106 {
Moran Peker17e36e12018-05-02 12:55:20 +03001107 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001108 }
Moran Peker60364322018-04-29 11:34:58 +03001109 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001110 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001111 /* If data_size is 0 then data may be NULL and then the
1112 * call to memset would have undefined behavior. */
1113 if( data_size != 0 )
1114 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001115 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001116 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001117 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1118 * Move the data to the beginning and erase remaining data
1119 * at the original location. */
1120 if( 2 * (size_t) ret <= data_size )
1121 {
1122 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001123 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001124 }
1125 else if( (size_t) ret < data_size )
1126 {
1127 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001128 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001129 }
Moran Pekera998bc62018-04-16 18:16:20 +03001130 *data_length = ret;
1131 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001132 }
1133 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001134#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001135 {
1136 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001137 it is valid for a special-purpose implementation to omit
1138 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001139 return( PSA_ERROR_NOT_SUPPORTED );
1140 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001141 }
1142}
1143
Gilles Peskinec5487a82018-12-03 18:08:14 +01001144psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001145 uint8_t *data,
1146 size_t data_size,
1147 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001148{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001149 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001150 psa_status_t status;
1151
1152 /* Set the key to empty now, so that even when there are errors, we always
1153 * set data_length to a value between 0 and data_size. On error, setting
1154 * the key to empty is a good choice because an empty key representation is
1155 * unlikely to be accepted anywhere. */
1156 *data_length = 0;
1157
1158 /* Export requires the EXPORT flag. There is an exception for public keys,
1159 * which don't require any flag, but psa_get_key_from_slot takes
1160 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001161 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001162 if( status != PSA_SUCCESS )
1163 return( status );
1164 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001165 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001166}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001167
Gilles Peskinec5487a82018-12-03 18:08:14 +01001168psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001169 uint8_t *data,
1170 size_t data_size,
1171 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001172{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001173 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001174 psa_status_t status;
1175
1176 /* Set the key to empty now, so that even when there are errors, we always
1177 * set data_length to a value between 0 and data_size. On error, setting
1178 * the key to empty is a good choice because an empty key representation is
1179 * unlikely to be accepted anywhere. */
1180 *data_length = 0;
1181
1182 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001183 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001184 if( status != PSA_SUCCESS )
1185 return( status );
1186 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001187 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001188}
1189
Darryl Green0c6575a2018-11-07 16:05:30 +00001190#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine2f060a82018-12-04 17:12:32 +01001191static psa_status_t psa_save_generated_persistent_key( psa_key_slot_t *slot,
Darryl Green0c6575a2018-11-07 16:05:30 +00001192 size_t bits )
1193{
1194 psa_status_t status;
1195 uint8_t *data;
1196 size_t key_length;
1197 size_t data_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type, bits );
1198 data = mbedtls_calloc( 1, data_size );
itayzafrir910c76b2018-11-21 16:03:21 +02001199 if( data == NULL )
1200 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Darryl Green0c6575a2018-11-07 16:05:30 +00001201 /* Get key data in export format */
1202 status = psa_internal_export_key( slot, data, data_size, &key_length, 0 );
1203 if( status != PSA_SUCCESS )
1204 {
1205 slot->type = PSA_KEY_TYPE_NONE;
1206 goto exit;
1207 }
1208 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +01001209 status = psa_save_persistent_key( slot->persistent_storage_id,
1210 slot->type, &slot->policy,
Darryl Green0c6575a2018-11-07 16:05:30 +00001211 data, key_length );
1212 if( status != PSA_SUCCESS )
1213 {
1214 slot->type = PSA_KEY_TYPE_NONE;
1215 }
1216exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01001217 mbedtls_platform_zeroize( data, key_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00001218 mbedtls_free( data );
1219 return( status );
1220}
1221#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1222
Gilles Peskinef603c712019-01-19 13:40:11 +01001223static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
1224 psa_key_handle_t target )
1225{
1226 psa_status_t status;
1227 uint8_t *buffer = NULL;
1228 size_t buffer_size = 0;
1229 size_t length;
1230
1231 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->type,
1232 psa_get_key_bits( source ) );
1233 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001234 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001235 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01001236 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1237 if( status != PSA_SUCCESS )
1238 goto exit;
1239 status = psa_import_key( target, source->type, buffer, length );
1240
1241exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001242 if( buffer_size != 0 )
1243 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001244 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01001245 return( status );
1246}
1247
1248psa_status_t psa_copy_key(psa_key_handle_t source_handle,
1249 psa_key_handle_t target_handle,
1250 const psa_key_policy_t *constraint)
1251{
1252 psa_key_slot_t *source_slot = NULL;
1253 psa_key_slot_t *target_slot = NULL;
1254 psa_key_policy_t new_policy;
1255 psa_status_t status;
1256 status = psa_get_key_from_slot( source_handle, &source_slot, 0, 0 );
1257 if( status != PSA_SUCCESS )
1258 return( status );
1259 status = psa_get_empty_key_slot( target_handle, &target_slot );
1260 if( status != PSA_SUCCESS )
1261 return( status );
1262
1263 new_policy = target_slot->policy;
1264 status = psa_restrict_key_policy( &new_policy, &source_slot->policy );
1265 if( status != PSA_SUCCESS )
1266 return( status );
1267 if( constraint != NULL )
1268 {
1269 status = psa_restrict_key_policy( &new_policy, constraint );
1270 if( status != PSA_SUCCESS )
1271 return( status );
1272 }
1273
1274 status = psa_copy_key_material( source_slot, target_handle );
1275 if( status != PSA_SUCCESS )
1276 return( status );
1277
1278 target_slot->policy = new_policy;
1279 return( PSA_SUCCESS );
1280}
1281
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001282
1283
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001284/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001285/* Message digests */
1286/****************************************************************/
1287
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001288static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001289{
1290 switch( alg )
1291 {
1292#if defined(MBEDTLS_MD2_C)
1293 case PSA_ALG_MD2:
1294 return( &mbedtls_md2_info );
1295#endif
1296#if defined(MBEDTLS_MD4_C)
1297 case PSA_ALG_MD4:
1298 return( &mbedtls_md4_info );
1299#endif
1300#if defined(MBEDTLS_MD5_C)
1301 case PSA_ALG_MD5:
1302 return( &mbedtls_md5_info );
1303#endif
1304#if defined(MBEDTLS_RIPEMD160_C)
1305 case PSA_ALG_RIPEMD160:
1306 return( &mbedtls_ripemd160_info );
1307#endif
1308#if defined(MBEDTLS_SHA1_C)
1309 case PSA_ALG_SHA_1:
1310 return( &mbedtls_sha1_info );
1311#endif
1312#if defined(MBEDTLS_SHA256_C)
1313 case PSA_ALG_SHA_224:
1314 return( &mbedtls_sha224_info );
1315 case PSA_ALG_SHA_256:
1316 return( &mbedtls_sha256_info );
1317#endif
1318#if defined(MBEDTLS_SHA512_C)
1319 case PSA_ALG_SHA_384:
1320 return( &mbedtls_sha384_info );
1321 case PSA_ALG_SHA_512:
1322 return( &mbedtls_sha512_info );
1323#endif
1324 default:
1325 return( NULL );
1326 }
1327}
1328
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001329psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1330{
1331 switch( operation->alg )
1332 {
Gilles Peskine81736312018-06-26 15:04:31 +02001333 case 0:
1334 /* The object has (apparently) been initialized but it is not
1335 * in use. It's ok to call abort on such an object, and there's
1336 * nothing to do. */
1337 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001338#if defined(MBEDTLS_MD2_C)
1339 case PSA_ALG_MD2:
1340 mbedtls_md2_free( &operation->ctx.md2 );
1341 break;
1342#endif
1343#if defined(MBEDTLS_MD4_C)
1344 case PSA_ALG_MD4:
1345 mbedtls_md4_free( &operation->ctx.md4 );
1346 break;
1347#endif
1348#if defined(MBEDTLS_MD5_C)
1349 case PSA_ALG_MD5:
1350 mbedtls_md5_free( &operation->ctx.md5 );
1351 break;
1352#endif
1353#if defined(MBEDTLS_RIPEMD160_C)
1354 case PSA_ALG_RIPEMD160:
1355 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1356 break;
1357#endif
1358#if defined(MBEDTLS_SHA1_C)
1359 case PSA_ALG_SHA_1:
1360 mbedtls_sha1_free( &operation->ctx.sha1 );
1361 break;
1362#endif
1363#if defined(MBEDTLS_SHA256_C)
1364 case PSA_ALG_SHA_224:
1365 case PSA_ALG_SHA_256:
1366 mbedtls_sha256_free( &operation->ctx.sha256 );
1367 break;
1368#endif
1369#if defined(MBEDTLS_SHA512_C)
1370 case PSA_ALG_SHA_384:
1371 case PSA_ALG_SHA_512:
1372 mbedtls_sha512_free( &operation->ctx.sha512 );
1373 break;
1374#endif
1375 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001376 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001377 }
1378 operation->alg = 0;
1379 return( PSA_SUCCESS );
1380}
1381
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001382psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001383 psa_algorithm_t alg )
1384{
1385 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001386
1387 /* A context must be freshly initialized before it can be set up. */
1388 if( operation->alg != 0 )
1389 {
1390 return( PSA_ERROR_BAD_STATE );
1391 }
1392
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001393 switch( alg )
1394 {
1395#if defined(MBEDTLS_MD2_C)
1396 case PSA_ALG_MD2:
1397 mbedtls_md2_init( &operation->ctx.md2 );
1398 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1399 break;
1400#endif
1401#if defined(MBEDTLS_MD4_C)
1402 case PSA_ALG_MD4:
1403 mbedtls_md4_init( &operation->ctx.md4 );
1404 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1405 break;
1406#endif
1407#if defined(MBEDTLS_MD5_C)
1408 case PSA_ALG_MD5:
1409 mbedtls_md5_init( &operation->ctx.md5 );
1410 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1411 break;
1412#endif
1413#if defined(MBEDTLS_RIPEMD160_C)
1414 case PSA_ALG_RIPEMD160:
1415 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1416 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1417 break;
1418#endif
1419#if defined(MBEDTLS_SHA1_C)
1420 case PSA_ALG_SHA_1:
1421 mbedtls_sha1_init( &operation->ctx.sha1 );
1422 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1423 break;
1424#endif
1425#if defined(MBEDTLS_SHA256_C)
1426 case PSA_ALG_SHA_224:
1427 mbedtls_sha256_init( &operation->ctx.sha256 );
1428 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1429 break;
1430 case PSA_ALG_SHA_256:
1431 mbedtls_sha256_init( &operation->ctx.sha256 );
1432 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1433 break;
1434#endif
1435#if defined(MBEDTLS_SHA512_C)
1436 case PSA_ALG_SHA_384:
1437 mbedtls_sha512_init( &operation->ctx.sha512 );
1438 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1439 break;
1440 case PSA_ALG_SHA_512:
1441 mbedtls_sha512_init( &operation->ctx.sha512 );
1442 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1443 break;
1444#endif
1445 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001446 return( PSA_ALG_IS_HASH( alg ) ?
1447 PSA_ERROR_NOT_SUPPORTED :
1448 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001449 }
1450 if( ret == 0 )
1451 operation->alg = alg;
1452 else
1453 psa_hash_abort( operation );
1454 return( mbedtls_to_psa_error( ret ) );
1455}
1456
1457psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1458 const uint8_t *input,
1459 size_t input_length )
1460{
1461 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001462
1463 /* Don't require hash implementations to behave correctly on a
1464 * zero-length input, which may have an invalid pointer. */
1465 if( input_length == 0 )
1466 return( PSA_SUCCESS );
1467
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001468 switch( operation->alg )
1469 {
1470#if defined(MBEDTLS_MD2_C)
1471 case PSA_ALG_MD2:
1472 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1473 input, input_length );
1474 break;
1475#endif
1476#if defined(MBEDTLS_MD4_C)
1477 case PSA_ALG_MD4:
1478 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1479 input, input_length );
1480 break;
1481#endif
1482#if defined(MBEDTLS_MD5_C)
1483 case PSA_ALG_MD5:
1484 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1485 input, input_length );
1486 break;
1487#endif
1488#if defined(MBEDTLS_RIPEMD160_C)
1489 case PSA_ALG_RIPEMD160:
1490 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1491 input, input_length );
1492 break;
1493#endif
1494#if defined(MBEDTLS_SHA1_C)
1495 case PSA_ALG_SHA_1:
1496 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1497 input, input_length );
1498 break;
1499#endif
1500#if defined(MBEDTLS_SHA256_C)
1501 case PSA_ALG_SHA_224:
1502 case PSA_ALG_SHA_256:
1503 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1504 input, input_length );
1505 break;
1506#endif
1507#if defined(MBEDTLS_SHA512_C)
1508 case PSA_ALG_SHA_384:
1509 case PSA_ALG_SHA_512:
1510 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1511 input, input_length );
1512 break;
1513#endif
1514 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001515 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001516 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001517
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001518 if( ret != 0 )
1519 psa_hash_abort( operation );
1520 return( mbedtls_to_psa_error( ret ) );
1521}
1522
1523psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1524 uint8_t *hash,
1525 size_t hash_size,
1526 size_t *hash_length )
1527{
itayzafrir40835d42018-08-02 13:14:17 +03001528 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001529 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001530 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001531
1532 /* Fill the output buffer with something that isn't a valid hash
1533 * (barring an attack on the hash and deliberately-crafted input),
1534 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001535 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001536 /* If hash_size is 0 then hash may be NULL and then the
1537 * call to memset would have undefined behavior. */
1538 if( hash_size != 0 )
1539 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001540
1541 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001542 {
1543 status = PSA_ERROR_BUFFER_TOO_SMALL;
1544 goto exit;
1545 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001546
1547 switch( operation->alg )
1548 {
1549#if defined(MBEDTLS_MD2_C)
1550 case PSA_ALG_MD2:
1551 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1552 break;
1553#endif
1554#if defined(MBEDTLS_MD4_C)
1555 case PSA_ALG_MD4:
1556 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1557 break;
1558#endif
1559#if defined(MBEDTLS_MD5_C)
1560 case PSA_ALG_MD5:
1561 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1562 break;
1563#endif
1564#if defined(MBEDTLS_RIPEMD160_C)
1565 case PSA_ALG_RIPEMD160:
1566 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1567 break;
1568#endif
1569#if defined(MBEDTLS_SHA1_C)
1570 case PSA_ALG_SHA_1:
1571 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1572 break;
1573#endif
1574#if defined(MBEDTLS_SHA256_C)
1575 case PSA_ALG_SHA_224:
1576 case PSA_ALG_SHA_256:
1577 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1578 break;
1579#endif
1580#if defined(MBEDTLS_SHA512_C)
1581 case PSA_ALG_SHA_384:
1582 case PSA_ALG_SHA_512:
1583 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1584 break;
1585#endif
1586 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001587 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001588 }
itayzafrir40835d42018-08-02 13:14:17 +03001589 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001590
itayzafrir40835d42018-08-02 13:14:17 +03001591exit:
1592 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001593 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001594 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001595 return( psa_hash_abort( operation ) );
1596 }
1597 else
1598 {
1599 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001600 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001601 }
1602}
1603
Gilles Peskine2d277862018-06-18 15:41:12 +02001604psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1605 const uint8_t *hash,
1606 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001607{
1608 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1609 size_t actual_hash_length;
1610 psa_status_t status = psa_hash_finish( operation,
1611 actual_hash, sizeof( actual_hash ),
1612 &actual_hash_length );
1613 if( status != PSA_SUCCESS )
1614 return( status );
1615 if( actual_hash_length != hash_length )
1616 return( PSA_ERROR_INVALID_SIGNATURE );
1617 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1618 return( PSA_ERROR_INVALID_SIGNATURE );
1619 return( PSA_SUCCESS );
1620}
1621
Gilles Peskineeb35d782019-01-22 17:56:16 +01001622psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
1623 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001624{
1625 if( target_operation->alg != 0 )
1626 return( PSA_ERROR_BAD_STATE );
1627
1628 switch( source_operation->alg )
1629 {
1630 case 0:
1631 return( PSA_ERROR_BAD_STATE );
1632#if defined(MBEDTLS_MD2_C)
1633 case PSA_ALG_MD2:
1634 mbedtls_md2_clone( &target_operation->ctx.md2,
1635 &source_operation->ctx.md2 );
1636 break;
1637#endif
1638#if defined(MBEDTLS_MD4_C)
1639 case PSA_ALG_MD4:
1640 mbedtls_md4_clone( &target_operation->ctx.md4,
1641 &source_operation->ctx.md4 );
1642 break;
1643#endif
1644#if defined(MBEDTLS_MD5_C)
1645 case PSA_ALG_MD5:
1646 mbedtls_md5_clone( &target_operation->ctx.md5,
1647 &source_operation->ctx.md5 );
1648 break;
1649#endif
1650#if defined(MBEDTLS_RIPEMD160_C)
1651 case PSA_ALG_RIPEMD160:
1652 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
1653 &source_operation->ctx.ripemd160 );
1654 break;
1655#endif
1656#if defined(MBEDTLS_SHA1_C)
1657 case PSA_ALG_SHA_1:
1658 mbedtls_sha1_clone( &target_operation->ctx.sha1,
1659 &source_operation->ctx.sha1 );
1660 break;
1661#endif
1662#if defined(MBEDTLS_SHA256_C)
1663 case PSA_ALG_SHA_224:
1664 case PSA_ALG_SHA_256:
1665 mbedtls_sha256_clone( &target_operation->ctx.sha256,
1666 &source_operation->ctx.sha256 );
1667 break;
1668#endif
1669#if defined(MBEDTLS_SHA512_C)
1670 case PSA_ALG_SHA_384:
1671 case PSA_ALG_SHA_512:
1672 mbedtls_sha512_clone( &target_operation->ctx.sha512,
1673 &source_operation->ctx.sha512 );
1674 break;
1675#endif
1676 default:
1677 return( PSA_ERROR_NOT_SUPPORTED );
1678 }
1679
1680 target_operation->alg = source_operation->alg;
1681 return( PSA_SUCCESS );
1682}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001683
1684
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001685/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001686/* MAC */
1687/****************************************************************/
1688
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001689static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001690 psa_algorithm_t alg,
1691 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001692 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001693 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001694{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001695 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001696 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001697
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001698 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001699 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001700
Gilles Peskine8c9def32018-02-08 10:02:12 +01001701 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1702 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001703 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001704 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001705 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001706 mode = MBEDTLS_MODE_STREAM;
1707 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001708 case PSA_ALG_CTR:
1709 mode = MBEDTLS_MODE_CTR;
1710 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001711 case PSA_ALG_CFB:
1712 mode = MBEDTLS_MODE_CFB;
1713 break;
1714 case PSA_ALG_OFB:
1715 mode = MBEDTLS_MODE_OFB;
1716 break;
1717 case PSA_ALG_CBC_NO_PADDING:
1718 mode = MBEDTLS_MODE_CBC;
1719 break;
1720 case PSA_ALG_CBC_PKCS7:
1721 mode = MBEDTLS_MODE_CBC;
1722 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001723 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001724 mode = MBEDTLS_MODE_CCM;
1725 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001726 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001727 mode = MBEDTLS_MODE_GCM;
1728 break;
1729 default:
1730 return( NULL );
1731 }
1732 }
1733 else if( alg == PSA_ALG_CMAC )
1734 mode = MBEDTLS_MODE_ECB;
1735 else if( alg == PSA_ALG_GMAC )
1736 mode = MBEDTLS_MODE_GCM;
1737 else
1738 return( NULL );
1739
1740 switch( key_type )
1741 {
1742 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001743 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001744 break;
1745 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001746 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1747 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001748 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001749 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001750 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001751 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001752 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1753 * but two-key Triple-DES is functionally three-key Triple-DES
1754 * with K1=K3, so that's how we present it to mbedtls. */
1755 if( key_bits == 128 )
1756 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001757 break;
1758 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001759 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001760 break;
1761 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001762 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001763 break;
1764 default:
1765 return( NULL );
1766 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001767 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001768 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001769
Jaeden Amero23bbb752018-06-26 14:16:54 +01001770 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1771 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001772}
1773
Gilles Peskinea05219c2018-11-16 16:02:56 +01001774#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001775static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001776{
Gilles Peskine2d277862018-06-18 15:41:12 +02001777 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001778 {
1779 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001780 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001781 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001782 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001783 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001784 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001785 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001786 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001787 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001788 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001789 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001790 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001791 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001792 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001793 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001794 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001795 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001796 return( 128 );
1797 default:
1798 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001799 }
1800}
Gilles Peskinea05219c2018-11-16 16:02:56 +01001801#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001802
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001803/* Initialize the MAC operation structure. Once this function has been
1804 * called, psa_mac_abort can run and will do the right thing. */
1805static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1806 psa_algorithm_t alg )
1807{
1808 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1809
1810 operation->alg = alg;
1811 operation->key_set = 0;
1812 operation->iv_set = 0;
1813 operation->iv_required = 0;
1814 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001815 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001816
1817#if defined(MBEDTLS_CMAC_C)
1818 if( alg == PSA_ALG_CMAC )
1819 {
1820 operation->iv_required = 0;
1821 mbedtls_cipher_init( &operation->ctx.cmac );
1822 status = PSA_SUCCESS;
1823 }
1824 else
1825#endif /* MBEDTLS_CMAC_C */
1826#if defined(MBEDTLS_MD_C)
1827 if( PSA_ALG_IS_HMAC( operation->alg ) )
1828 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001829 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1830 operation->ctx.hmac.hash_ctx.alg = 0;
1831 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001832 }
1833 else
1834#endif /* MBEDTLS_MD_C */
1835 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001836 if( ! PSA_ALG_IS_MAC( alg ) )
1837 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001838 }
1839
1840 if( status != PSA_SUCCESS )
1841 memset( operation, 0, sizeof( *operation ) );
1842 return( status );
1843}
1844
Gilles Peskine01126fa2018-07-12 17:04:55 +02001845#if defined(MBEDTLS_MD_C)
1846static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1847{
Gilles Peskine3f108122018-12-07 18:14:53 +01001848 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001849 return( psa_hash_abort( &hmac->hash_ctx ) );
1850}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01001851
1852static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
1853{
1854 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
1855 memset( hmac, 0, sizeof( *hmac ) );
1856}
Gilles Peskine01126fa2018-07-12 17:04:55 +02001857#endif /* MBEDTLS_MD_C */
1858
Gilles Peskine8c9def32018-02-08 10:02:12 +01001859psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1860{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001861 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001862 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001863 /* The object has (apparently) been initialized but it is not
1864 * in use. It's ok to call abort on such an object, and there's
1865 * nothing to do. */
1866 return( PSA_SUCCESS );
1867 }
1868 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001869#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001870 if( operation->alg == PSA_ALG_CMAC )
1871 {
1872 mbedtls_cipher_free( &operation->ctx.cmac );
1873 }
1874 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001875#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001876#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001877 if( PSA_ALG_IS_HMAC( operation->alg ) )
1878 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001879 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001880 }
1881 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001882#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001883 {
1884 /* Sanity check (shouldn't happen: operation->alg should
1885 * always have been initialized to a valid value). */
1886 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001887 }
Moran Peker41deec42018-04-04 15:43:05 +03001888
Gilles Peskine8c9def32018-02-08 10:02:12 +01001889 operation->alg = 0;
1890 operation->key_set = 0;
1891 operation->iv_set = 0;
1892 operation->iv_required = 0;
1893 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001894 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001895
Gilles Peskine8c9def32018-02-08 10:02:12 +01001896 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001897
1898bad_state:
1899 /* If abort is called on an uninitialized object, we can't trust
1900 * anything. Wipe the object in case it contains confidential data.
1901 * This may result in a memory leak if a pointer gets overwritten,
1902 * but it's too late to do anything about this. */
1903 memset( operation, 0, sizeof( *operation ) );
1904 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001905}
1906
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001907#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001908static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001909 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001910 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001911 const mbedtls_cipher_info_t *cipher_info )
1912{
1913 int ret;
1914
1915 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001916
1917 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1918 if( ret != 0 )
1919 return( ret );
1920
1921 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1922 slot->data.raw.data,
1923 key_bits );
1924 return( ret );
1925}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001926#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001927
Gilles Peskine248051a2018-06-20 16:09:38 +02001928#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001929static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1930 const uint8_t *key,
1931 size_t key_length,
1932 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001933{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001934 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001935 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001936 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001937 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001938 psa_status_t status;
1939
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001940 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1941 * overflow below. This should never trigger if the hash algorithm
1942 * is implemented correctly. */
1943 /* The size checks against the ipad and opad buffers cannot be written
1944 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1945 * because that triggers -Wlogical-op on GCC 7.3. */
1946 if( block_size > sizeof( ipad ) )
1947 return( PSA_ERROR_NOT_SUPPORTED );
1948 if( block_size > sizeof( hmac->opad ) )
1949 return( PSA_ERROR_NOT_SUPPORTED );
1950 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001951 return( PSA_ERROR_NOT_SUPPORTED );
1952
Gilles Peskined223b522018-06-11 18:12:58 +02001953 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001954 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001955 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001956 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001957 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001958 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001959 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001960 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001961 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001962 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001963 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001964 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001965 }
Gilles Peskine96889972018-07-12 17:07:03 +02001966 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1967 * but it is permitted. It is common when HMAC is used in HKDF, for
1968 * example. Don't call `memcpy` in the 0-length because `key` could be
1969 * an invalid pointer which would make the behavior undefined. */
1970 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001971 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001972
Gilles Peskined223b522018-06-11 18:12:58 +02001973 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1974 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001975 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001976 ipad[i] ^= 0x36;
1977 memset( ipad + key_length, 0x36, block_size - key_length );
1978
1979 /* Copy the key material from ipad to opad, flipping the requisite bits,
1980 * and filling the rest of opad with the requisite constant. */
1981 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001982 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1983 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001984
Gilles Peskine01126fa2018-07-12 17:04:55 +02001985 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001986 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001987 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001988
Gilles Peskine01126fa2018-07-12 17:04:55 +02001989 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001990
1991cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01001992 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001993
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001994 return( status );
1995}
Gilles Peskine248051a2018-06-20 16:09:38 +02001996#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001997
Gilles Peskine89167cb2018-07-08 20:12:23 +02001998static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001999 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002000 psa_algorithm_t alg,
2001 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002002{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002003 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002004 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002005 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002006 psa_key_usage_t usage =
2007 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02002008 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002009 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002010
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002011 /* A context must be freshly initialized before it can be set up. */
2012 if( operation->alg != 0 )
2013 {
2014 return( PSA_ERROR_BAD_STATE );
2015 }
2016
Gilles Peskined911eb72018-08-14 15:18:45 +02002017 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002018 if( status != PSA_SUCCESS )
2019 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002020 if( is_sign )
2021 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002022
Gilles Peskinec5487a82018-12-03 18:08:14 +01002023 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002024 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002025 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002026 key_bits = psa_get_key_bits( slot );
2027
Gilles Peskine8c9def32018-02-08 10:02:12 +01002028#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002029 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002030 {
2031 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002032 mbedtls_cipher_info_from_psa( full_length_alg,
2033 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002034 int ret;
2035 if( cipher_info == NULL )
2036 {
2037 status = PSA_ERROR_NOT_SUPPORTED;
2038 goto exit;
2039 }
2040 operation->mac_size = cipher_info->block_size;
2041 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2042 status = mbedtls_to_psa_error( ret );
2043 }
2044 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002045#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002046#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002047 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002048 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002049 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002050 if( hash_alg == 0 )
2051 {
2052 status = PSA_ERROR_NOT_SUPPORTED;
2053 goto exit;
2054 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002055
2056 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2057 /* Sanity check. This shouldn't fail on a valid configuration. */
2058 if( operation->mac_size == 0 ||
2059 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2060 {
2061 status = PSA_ERROR_NOT_SUPPORTED;
2062 goto exit;
2063 }
2064
Gilles Peskine01126fa2018-07-12 17:04:55 +02002065 if( slot->type != PSA_KEY_TYPE_HMAC )
2066 {
2067 status = PSA_ERROR_INVALID_ARGUMENT;
2068 goto exit;
2069 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002070
Gilles Peskine01126fa2018-07-12 17:04:55 +02002071 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2072 slot->data.raw.data,
2073 slot->data.raw.bytes,
2074 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002075 }
2076 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002077#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002078 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002079 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002080 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002081 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002082
Gilles Peskined911eb72018-08-14 15:18:45 +02002083 if( truncated == 0 )
2084 {
2085 /* The "normal" case: untruncated algorithm. Nothing to do. */
2086 }
2087 else if( truncated < 4 )
2088 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002089 /* A very short MAC is too short for security since it can be
2090 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2091 * so we make this our minimum, even though 32 bits is still
2092 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002093 status = PSA_ERROR_NOT_SUPPORTED;
2094 }
2095 else if( truncated > operation->mac_size )
2096 {
2097 /* It's impossible to "truncate" to a larger length. */
2098 status = PSA_ERROR_INVALID_ARGUMENT;
2099 }
2100 else
2101 operation->mac_size = truncated;
2102
Gilles Peskinefbfac682018-07-08 20:51:54 +02002103exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002104 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002105 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002106 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002107 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002108 else
2109 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002110 operation->key_set = 1;
2111 }
2112 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002113}
2114
Gilles Peskine89167cb2018-07-08 20:12:23 +02002115psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002116 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002117 psa_algorithm_t alg )
2118{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002119 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002120}
2121
2122psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002123 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002124 psa_algorithm_t alg )
2125{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002126 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002127}
2128
Gilles Peskine8c9def32018-02-08 10:02:12 +01002129psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2130 const uint8_t *input,
2131 size_t input_length )
2132{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002133 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002134 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002135 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002136 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002137 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002138 operation->has_input = 1;
2139
Gilles Peskine8c9def32018-02-08 10:02:12 +01002140#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002141 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002142 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002143 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2144 input, input_length );
2145 status = mbedtls_to_psa_error( ret );
2146 }
2147 else
2148#endif /* MBEDTLS_CMAC_C */
2149#if defined(MBEDTLS_MD_C)
2150 if( PSA_ALG_IS_HMAC( operation->alg ) )
2151 {
2152 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2153 input_length );
2154 }
2155 else
2156#endif /* MBEDTLS_MD_C */
2157 {
2158 /* This shouldn't happen if `operation` was initialized by
2159 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002160 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002161 }
2162
Gilles Peskinefbfac682018-07-08 20:51:54 +02002163 if( status != PSA_SUCCESS )
2164 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002165 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002166}
2167
Gilles Peskine01126fa2018-07-12 17:04:55 +02002168#if defined(MBEDTLS_MD_C)
2169static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2170 uint8_t *mac,
2171 size_t mac_size )
2172{
2173 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2174 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2175 size_t hash_size = 0;
2176 size_t block_size = psa_get_hash_block_size( hash_alg );
2177 psa_status_t status;
2178
Gilles Peskine01126fa2018-07-12 17:04:55 +02002179 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2180 if( status != PSA_SUCCESS )
2181 return( status );
2182 /* From here on, tmp needs to be wiped. */
2183
2184 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2185 if( status != PSA_SUCCESS )
2186 goto exit;
2187
2188 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2189 if( status != PSA_SUCCESS )
2190 goto exit;
2191
2192 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2193 if( status != PSA_SUCCESS )
2194 goto exit;
2195
Gilles Peskined911eb72018-08-14 15:18:45 +02002196 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2197 if( status != PSA_SUCCESS )
2198 goto exit;
2199
2200 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002201
2202exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002203 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002204 return( status );
2205}
2206#endif /* MBEDTLS_MD_C */
2207
mohammad16036df908f2018-04-02 08:34:15 -07002208static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002209 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002210 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002211{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002212 if( ! operation->key_set )
2213 return( PSA_ERROR_BAD_STATE );
2214 if( operation->iv_required && ! operation->iv_set )
2215 return( PSA_ERROR_BAD_STATE );
2216
Gilles Peskine8c9def32018-02-08 10:02:12 +01002217 if( mac_size < operation->mac_size )
2218 return( PSA_ERROR_BUFFER_TOO_SMALL );
2219
Gilles Peskine8c9def32018-02-08 10:02:12 +01002220#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002221 if( operation->alg == PSA_ALG_CMAC )
2222 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002223 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2224 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2225 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002226 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002227 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002228 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002229 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002230 else
2231#endif /* MBEDTLS_CMAC_C */
2232#if defined(MBEDTLS_MD_C)
2233 if( PSA_ALG_IS_HMAC( operation->alg ) )
2234 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002235 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002236 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002237 }
2238 else
2239#endif /* MBEDTLS_MD_C */
2240 {
2241 /* This shouldn't happen if `operation` was initialized by
2242 * a setup function. */
2243 return( PSA_ERROR_BAD_STATE );
2244 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002245}
2246
Gilles Peskineacd4be32018-07-08 19:56:25 +02002247psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2248 uint8_t *mac,
2249 size_t mac_size,
2250 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002251{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002252 psa_status_t status;
2253
Jaeden Amero252ef282019-02-15 14:05:35 +00002254 if( operation->alg == 0 )
2255 {
2256 return( PSA_ERROR_BAD_STATE );
2257 }
2258
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002259 /* Fill the output buffer with something that isn't a valid mac
2260 * (barring an attack on the mac and deliberately-crafted input),
2261 * in case the caller doesn't check the return status properly. */
2262 *mac_length = mac_size;
2263 /* If mac_size is 0 then mac may be NULL and then the
2264 * call to memset would have undefined behavior. */
2265 if( mac_size != 0 )
2266 memset( mac, '!', mac_size );
2267
Gilles Peskine89167cb2018-07-08 20:12:23 +02002268 if( ! operation->is_sign )
2269 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002270 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002271 }
mohammad16036df908f2018-04-02 08:34:15 -07002272
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002273 status = psa_mac_finish_internal( operation, mac, mac_size );
2274
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002275 if( status == PSA_SUCCESS )
2276 {
2277 status = psa_mac_abort( operation );
2278 if( status == PSA_SUCCESS )
2279 *mac_length = operation->mac_size;
2280 else
2281 memset( mac, '!', mac_size );
2282 }
2283 else
2284 psa_mac_abort( operation );
2285 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002286}
2287
Gilles Peskineacd4be32018-07-08 19:56:25 +02002288psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2289 const uint8_t *mac,
2290 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002291{
Gilles Peskine828ed142018-06-18 23:25:51 +02002292 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002293 psa_status_t status;
2294
Jaeden Amero252ef282019-02-15 14:05:35 +00002295 if( operation->alg == 0 )
2296 {
2297 return( PSA_ERROR_BAD_STATE );
2298 }
2299
Gilles Peskine89167cb2018-07-08 20:12:23 +02002300 if( operation->is_sign )
2301 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002302 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002303 }
2304 if( operation->mac_size != mac_length )
2305 {
2306 status = PSA_ERROR_INVALID_SIGNATURE;
2307 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002308 }
mohammad16036df908f2018-04-02 08:34:15 -07002309
2310 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002311 actual_mac, sizeof( actual_mac ) );
2312
2313 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2314 status = PSA_ERROR_INVALID_SIGNATURE;
2315
2316cleanup:
2317 if( status == PSA_SUCCESS )
2318 status = psa_mac_abort( operation );
2319 else
2320 psa_mac_abort( operation );
2321
Gilles Peskine3f108122018-12-07 18:14:53 +01002322 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002323
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002324 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002325}
2326
2327
Gilles Peskine20035e32018-02-03 22:44:14 +01002328
Gilles Peskine20035e32018-02-03 22:44:14 +01002329/****************************************************************/
2330/* Asymmetric cryptography */
2331/****************************************************************/
2332
Gilles Peskine2b450e32018-06-27 15:42:46 +02002333#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002334/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002335 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002336static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2337 size_t hash_length,
2338 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002339{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002340 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002341 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002342 *md_alg = mbedtls_md_get_type( md_info );
2343
2344 /* The Mbed TLS RSA module uses an unsigned int for hash length
2345 * parameters. Validate that it fits so that we don't risk an
2346 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002347#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002348 if( hash_length > UINT_MAX )
2349 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002350#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002351
2352#if defined(MBEDTLS_PKCS1_V15)
2353 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2354 * must be correct. */
2355 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2356 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002357 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002358 if( md_info == NULL )
2359 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002360 if( mbedtls_md_get_size( md_info ) != hash_length )
2361 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002362 }
2363#endif /* MBEDTLS_PKCS1_V15 */
2364
2365#if defined(MBEDTLS_PKCS1_V21)
2366 /* PSS requires a hash internally. */
2367 if( PSA_ALG_IS_RSA_PSS( alg ) )
2368 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002369 if( md_info == NULL )
2370 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002371 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002372#endif /* MBEDTLS_PKCS1_V21 */
2373
Gilles Peskine61b91d42018-06-08 16:09:36 +02002374 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002375}
2376
Gilles Peskine2b450e32018-06-27 15:42:46 +02002377static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2378 psa_algorithm_t alg,
2379 const uint8_t *hash,
2380 size_t hash_length,
2381 uint8_t *signature,
2382 size_t signature_size,
2383 size_t *signature_length )
2384{
2385 psa_status_t status;
2386 int ret;
2387 mbedtls_md_type_t md_alg;
2388
2389 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2390 if( status != PSA_SUCCESS )
2391 return( status );
2392
Gilles Peskine630a18a2018-06-29 17:49:35 +02002393 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002394 return( PSA_ERROR_BUFFER_TOO_SMALL );
2395
2396#if defined(MBEDTLS_PKCS1_V15)
2397 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2398 {
2399 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2400 MBEDTLS_MD_NONE );
2401 ret = mbedtls_rsa_pkcs1_sign( rsa,
2402 mbedtls_ctr_drbg_random,
2403 &global_data.ctr_drbg,
2404 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002405 md_alg,
2406 (unsigned int) hash_length,
2407 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002408 signature );
2409 }
2410 else
2411#endif /* MBEDTLS_PKCS1_V15 */
2412#if defined(MBEDTLS_PKCS1_V21)
2413 if( PSA_ALG_IS_RSA_PSS( alg ) )
2414 {
2415 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2416 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2417 mbedtls_ctr_drbg_random,
2418 &global_data.ctr_drbg,
2419 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002420 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002421 (unsigned int) hash_length,
2422 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002423 signature );
2424 }
2425 else
2426#endif /* MBEDTLS_PKCS1_V21 */
2427 {
2428 return( PSA_ERROR_INVALID_ARGUMENT );
2429 }
2430
2431 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002432 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002433 return( mbedtls_to_psa_error( ret ) );
2434}
2435
2436static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2437 psa_algorithm_t alg,
2438 const uint8_t *hash,
2439 size_t hash_length,
2440 const uint8_t *signature,
2441 size_t signature_length )
2442{
2443 psa_status_t status;
2444 int ret;
2445 mbedtls_md_type_t md_alg;
2446
2447 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2448 if( status != PSA_SUCCESS )
2449 return( status );
2450
Gilles Peskine630a18a2018-06-29 17:49:35 +02002451 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002452 return( PSA_ERROR_BUFFER_TOO_SMALL );
2453
2454#if defined(MBEDTLS_PKCS1_V15)
2455 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2456 {
2457 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2458 MBEDTLS_MD_NONE );
2459 ret = mbedtls_rsa_pkcs1_verify( rsa,
2460 mbedtls_ctr_drbg_random,
2461 &global_data.ctr_drbg,
2462 MBEDTLS_RSA_PUBLIC,
2463 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002464 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002465 hash,
2466 signature );
2467 }
2468 else
2469#endif /* MBEDTLS_PKCS1_V15 */
2470#if defined(MBEDTLS_PKCS1_V21)
2471 if( PSA_ALG_IS_RSA_PSS( alg ) )
2472 {
2473 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2474 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2475 mbedtls_ctr_drbg_random,
2476 &global_data.ctr_drbg,
2477 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002478 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002479 (unsigned int) hash_length,
2480 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002481 signature );
2482 }
2483 else
2484#endif /* MBEDTLS_PKCS1_V21 */
2485 {
2486 return( PSA_ERROR_INVALID_ARGUMENT );
2487 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002488
2489 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2490 * the rest of the signature is invalid". This has little use in
2491 * practice and PSA doesn't report this distinction. */
2492 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2493 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002494 return( mbedtls_to_psa_error( ret ) );
2495}
2496#endif /* MBEDTLS_RSA_C */
2497
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002498#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002499/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2500 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2501 * (even though these functions don't modify it). */
2502static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2503 psa_algorithm_t alg,
2504 const uint8_t *hash,
2505 size_t hash_length,
2506 uint8_t *signature,
2507 size_t signature_size,
2508 size_t *signature_length )
2509{
2510 int ret;
2511 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002512 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002513 mbedtls_mpi_init( &r );
2514 mbedtls_mpi_init( &s );
2515
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002516 if( signature_size < 2 * curve_bytes )
2517 {
2518 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2519 goto cleanup;
2520 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002521
Gilles Peskinea05219c2018-11-16 16:02:56 +01002522#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002523 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2524 {
2525 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2526 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2527 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2528 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2529 hash, hash_length,
2530 md_alg ) );
2531 }
2532 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002533#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002534 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002535 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002536 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2537 hash, hash_length,
2538 mbedtls_ctr_drbg_random,
2539 &global_data.ctr_drbg ) );
2540 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002541
2542 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2543 signature,
2544 curve_bytes ) );
2545 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2546 signature + curve_bytes,
2547 curve_bytes ) );
2548
2549cleanup:
2550 mbedtls_mpi_free( &r );
2551 mbedtls_mpi_free( &s );
2552 if( ret == 0 )
2553 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002554 return( mbedtls_to_psa_error( ret ) );
2555}
2556
2557static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2558 const uint8_t *hash,
2559 size_t hash_length,
2560 const uint8_t *signature,
2561 size_t signature_length )
2562{
2563 int ret;
2564 mbedtls_mpi r, s;
2565 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2566 mbedtls_mpi_init( &r );
2567 mbedtls_mpi_init( &s );
2568
2569 if( signature_length != 2 * curve_bytes )
2570 return( PSA_ERROR_INVALID_SIGNATURE );
2571
2572 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2573 signature,
2574 curve_bytes ) );
2575 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2576 signature + curve_bytes,
2577 curve_bytes ) );
2578
2579 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2580 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002581
2582cleanup:
2583 mbedtls_mpi_free( &r );
2584 mbedtls_mpi_free( &s );
2585 return( mbedtls_to_psa_error( ret ) );
2586}
2587#endif /* MBEDTLS_ECDSA_C */
2588
Gilles Peskinec5487a82018-12-03 18:08:14 +01002589psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002590 psa_algorithm_t alg,
2591 const uint8_t *hash,
2592 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002593 uint8_t *signature,
2594 size_t signature_size,
2595 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002596{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002597 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002598 psa_status_t status;
2599
2600 *signature_length = signature_size;
2601
Gilles Peskinec5487a82018-12-03 18:08:14 +01002602 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002603 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002604 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002605 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002606 {
2607 status = PSA_ERROR_INVALID_ARGUMENT;
2608 goto exit;
2609 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002610
Gilles Peskine20035e32018-02-03 22:44:14 +01002611#if defined(MBEDTLS_RSA_C)
2612 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2613 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002614 status = psa_rsa_sign( slot->data.rsa,
2615 alg,
2616 hash, hash_length,
2617 signature, signature_size,
2618 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002619 }
2620 else
2621#endif /* defined(MBEDTLS_RSA_C) */
2622#if defined(MBEDTLS_ECP_C)
2623 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2624 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002625#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002626 if(
2627#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2628 PSA_ALG_IS_ECDSA( alg )
2629#else
2630 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2631#endif
2632 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002633 status = psa_ecdsa_sign( slot->data.ecp,
2634 alg,
2635 hash, hash_length,
2636 signature, signature_size,
2637 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002638 else
2639#endif /* defined(MBEDTLS_ECDSA_C) */
2640 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002641 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002642 }
itayzafrir5c753392018-05-08 11:18:38 +03002643 }
2644 else
2645#endif /* defined(MBEDTLS_ECP_C) */
2646 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002647 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002648 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002649
2650exit:
2651 /* Fill the unused part of the output buffer (the whole buffer on error,
2652 * the trailing part on success) with something that isn't a valid mac
2653 * (barring an attack on the mac and deliberately-crafted input),
2654 * in case the caller doesn't check the return status properly. */
2655 if( status == PSA_SUCCESS )
2656 memset( signature + *signature_length, '!',
2657 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002658 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002659 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002660 /* If signature_size is 0 then we have nothing to do. We must not call
2661 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002662 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002663}
2664
Gilles Peskinec5487a82018-12-03 18:08:14 +01002665psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03002666 psa_algorithm_t alg,
2667 const uint8_t *hash,
2668 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002669 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002670 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002671{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002672 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002673 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002674
Gilles Peskinec5487a82018-12-03 18:08:14 +01002675 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002676 if( status != PSA_SUCCESS )
2677 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002678
Gilles Peskine61b91d42018-06-08 16:09:36 +02002679#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002680 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002681 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002682 return( psa_rsa_verify( slot->data.rsa,
2683 alg,
2684 hash, hash_length,
2685 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002686 }
2687 else
2688#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002689#if defined(MBEDTLS_ECP_C)
2690 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2691 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002692#if defined(MBEDTLS_ECDSA_C)
2693 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002694 return( psa_ecdsa_verify( slot->data.ecp,
2695 hash, hash_length,
2696 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002697 else
2698#endif /* defined(MBEDTLS_ECDSA_C) */
2699 {
2700 return( PSA_ERROR_INVALID_ARGUMENT );
2701 }
itayzafrir5c753392018-05-08 11:18:38 +03002702 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002703 else
2704#endif /* defined(MBEDTLS_ECP_C) */
2705 {
2706 return( PSA_ERROR_NOT_SUPPORTED );
2707 }
2708}
2709
Gilles Peskine072ac562018-06-30 00:21:29 +02002710#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2711static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2712 mbedtls_rsa_context *rsa )
2713{
2714 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2715 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2716 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2717 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2718}
2719#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2720
Gilles Peskinec5487a82018-12-03 18:08:14 +01002721psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002722 psa_algorithm_t alg,
2723 const uint8_t *input,
2724 size_t input_length,
2725 const uint8_t *salt,
2726 size_t salt_length,
2727 uint8_t *output,
2728 size_t output_size,
2729 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002730{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002731 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002732 psa_status_t status;
2733
Darryl Green5cc689a2018-07-24 15:34:10 +01002734 (void) input;
2735 (void) input_length;
2736 (void) salt;
2737 (void) output;
2738 (void) output_size;
2739
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002740 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002741
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002742 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2743 return( PSA_ERROR_INVALID_ARGUMENT );
2744
Gilles Peskinec5487a82018-12-03 18:08:14 +01002745 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002746 if( status != PSA_SUCCESS )
2747 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002748 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2749 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002750 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002751
2752#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002753 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002754 {
2755 mbedtls_rsa_context *rsa = slot->data.rsa;
2756 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002757 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002758 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002759#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002760 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002761 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002762 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2763 mbedtls_ctr_drbg_random,
2764 &global_data.ctr_drbg,
2765 MBEDTLS_RSA_PUBLIC,
2766 input_length,
2767 input,
2768 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002769 }
2770 else
2771#endif /* MBEDTLS_PKCS1_V15 */
2772#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002773 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002774 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002775 psa_rsa_oaep_set_padding_mode( alg, rsa );
2776 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2777 mbedtls_ctr_drbg_random,
2778 &global_data.ctr_drbg,
2779 MBEDTLS_RSA_PUBLIC,
2780 salt, salt_length,
2781 input_length,
2782 input,
2783 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002784 }
2785 else
2786#endif /* MBEDTLS_PKCS1_V21 */
2787 {
2788 return( PSA_ERROR_INVALID_ARGUMENT );
2789 }
2790 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002791 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002792 return( mbedtls_to_psa_error( ret ) );
2793 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002794 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002795#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002796 {
2797 return( PSA_ERROR_NOT_SUPPORTED );
2798 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002799}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002800
Gilles Peskinec5487a82018-12-03 18:08:14 +01002801psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002802 psa_algorithm_t alg,
2803 const uint8_t *input,
2804 size_t input_length,
2805 const uint8_t *salt,
2806 size_t salt_length,
2807 uint8_t *output,
2808 size_t output_size,
2809 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002810{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002811 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002812 psa_status_t status;
2813
Darryl Green5cc689a2018-07-24 15:34:10 +01002814 (void) input;
2815 (void) input_length;
2816 (void) salt;
2817 (void) output;
2818 (void) output_size;
2819
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002820 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002821
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002822 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2823 return( PSA_ERROR_INVALID_ARGUMENT );
2824
Gilles Peskinec5487a82018-12-03 18:08:14 +01002825 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002826 if( status != PSA_SUCCESS )
2827 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002828 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2829 return( PSA_ERROR_INVALID_ARGUMENT );
2830
2831#if defined(MBEDTLS_RSA_C)
2832 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2833 {
2834 mbedtls_rsa_context *rsa = slot->data.rsa;
2835 int ret;
2836
Gilles Peskine630a18a2018-06-29 17:49:35 +02002837 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002838 return( PSA_ERROR_INVALID_ARGUMENT );
2839
2840#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002841 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002842 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002843 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2844 mbedtls_ctr_drbg_random,
2845 &global_data.ctr_drbg,
2846 MBEDTLS_RSA_PRIVATE,
2847 output_length,
2848 input,
2849 output,
2850 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002851 }
2852 else
2853#endif /* MBEDTLS_PKCS1_V15 */
2854#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002855 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002856 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002857 psa_rsa_oaep_set_padding_mode( alg, rsa );
2858 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2859 mbedtls_ctr_drbg_random,
2860 &global_data.ctr_drbg,
2861 MBEDTLS_RSA_PRIVATE,
2862 salt, salt_length,
2863 output_length,
2864 input,
2865 output,
2866 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002867 }
2868 else
2869#endif /* MBEDTLS_PKCS1_V21 */
2870 {
2871 return( PSA_ERROR_INVALID_ARGUMENT );
2872 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002873
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002874 return( mbedtls_to_psa_error( ret ) );
2875 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002876 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002877#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002878 {
2879 return( PSA_ERROR_NOT_SUPPORTED );
2880 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002881}
Gilles Peskine20035e32018-02-03 22:44:14 +01002882
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002883
2884
mohammad1603503973b2018-03-12 15:59:30 +02002885/****************************************************************/
2886/* Symmetric cryptography */
2887/****************************************************************/
2888
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002889/* Initialize the cipher operation structure. Once this function has been
2890 * called, psa_cipher_abort can run and will do the right thing. */
2891static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2892 psa_algorithm_t alg )
2893{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002894 if( ! PSA_ALG_IS_CIPHER( alg ) )
2895 {
2896 memset( operation, 0, sizeof( *operation ) );
2897 return( PSA_ERROR_INVALID_ARGUMENT );
2898 }
2899
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002900 operation->alg = alg;
2901 operation->key_set = 0;
2902 operation->iv_set = 0;
2903 operation->iv_required = 1;
2904 operation->iv_size = 0;
2905 operation->block_size = 0;
2906 mbedtls_cipher_init( &operation->ctx.cipher );
2907 return( PSA_SUCCESS );
2908}
2909
Gilles Peskinee553c652018-06-04 16:22:46 +02002910static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002911 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02002912 psa_algorithm_t alg,
2913 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002914{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01002915 int ret = 0;
2916 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002917 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002918 size_t key_bits;
2919 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002920 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2921 PSA_KEY_USAGE_ENCRYPT :
2922 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002923
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002924 /* A context must be freshly initialized before it can be set up. */
2925 if( operation->alg != 0 )
2926 {
2927 return( PSA_ERROR_BAD_STATE );
2928 }
2929
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002930 status = psa_cipher_init( operation, alg );
2931 if( status != PSA_SUCCESS )
2932 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002933
Gilles Peskinec5487a82018-12-03 18:08:14 +01002934 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002935 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01002936 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002937 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002938
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002939 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002940 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01002941 {
2942 status = PSA_ERROR_NOT_SUPPORTED;
2943 goto exit;
2944 }
mohammad1603503973b2018-03-12 15:59:30 +02002945
mohammad1603503973b2018-03-12 15:59:30 +02002946 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002947 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01002948 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002949
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002950#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002951 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002952 {
2953 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2954 unsigned char keys[24];
2955 memcpy( keys, slot->data.raw.data, 16 );
2956 memcpy( keys + 16, slot->data.raw.data, 8 );
2957 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2958 keys,
2959 192, cipher_operation );
2960 }
2961 else
2962#endif
2963 {
2964 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2965 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002966 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002967 }
Moran Peker41deec42018-04-04 15:43:05 +03002968 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01002969 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002970
mohammad16038481e742018-03-18 13:57:31 +02002971#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002972 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002973 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002974 case PSA_ALG_CBC_NO_PADDING:
2975 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2976 MBEDTLS_PADDING_NONE );
2977 break;
2978 case PSA_ALG_CBC_PKCS7:
2979 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2980 MBEDTLS_PADDING_PKCS7 );
2981 break;
2982 default:
2983 /* The algorithm doesn't involve padding. */
2984 ret = 0;
2985 break;
2986 }
2987 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01002988 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02002989#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2990
mohammad1603503973b2018-03-12 15:59:30 +02002991 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002992 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2993 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
2994 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02002995 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002996 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002997 }
mohammad1603503973b2018-03-12 15:59:30 +02002998
Gilles Peskine9ab61b62019-02-25 17:43:14 +01002999exit:
3000 if( status == 0 )
3001 status = mbedtls_to_psa_error( ret );
3002 if( status != 0 )
3003 psa_cipher_abort( operation );
3004 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003005}
3006
Gilles Peskinefe119512018-07-08 21:39:34 +02003007psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003008 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003009 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003010{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003011 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003012}
3013
Gilles Peskinefe119512018-07-08 21:39:34 +02003014psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003015 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003016 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003017{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003018 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003019}
3020
Gilles Peskinefe119512018-07-08 21:39:34 +02003021psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
3022 unsigned char *iv,
3023 size_t iv_size,
3024 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003025{
itayzafrir534bd7c2018-08-02 13:56:32 +03003026 psa_status_t status;
3027 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003028 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003029 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003030 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003031 }
Moran Peker41deec42018-04-04 15:43:05 +03003032 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003033 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003034 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003035 goto exit;
3036 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003037 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3038 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003039 if( ret != 0 )
3040 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003041 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003042 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003043 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003044
mohammad16038481e742018-03-18 13:57:31 +02003045 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003046 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003047
Moran Peker395db872018-05-31 14:07:14 +03003048exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003049 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003050 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003051 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003052}
3053
Gilles Peskinefe119512018-07-08 21:39:34 +02003054psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
3055 const unsigned char *iv,
3056 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003057{
itayzafrir534bd7c2018-08-02 13:56:32 +03003058 psa_status_t status;
3059 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003060 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003061 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003062 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003063 }
Moran Pekera28258c2018-05-29 16:25:04 +03003064 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003065 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003066 status = PSA_ERROR_INVALID_ARGUMENT;
3067 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003068 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003069 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3070 status = mbedtls_to_psa_error( ret );
3071exit:
3072 if( status == PSA_SUCCESS )
3073 operation->iv_set = 1;
3074 else
mohammad1603503973b2018-03-12 15:59:30 +02003075 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003076 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003077}
3078
Gilles Peskinee553c652018-06-04 16:22:46 +02003079psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3080 const uint8_t *input,
3081 size_t input_length,
3082 unsigned char *output,
3083 size_t output_size,
3084 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003085{
itayzafrir534bd7c2018-08-02 13:56:32 +03003086 psa_status_t status;
3087 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003088 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003089
3090 if( operation->alg == 0 )
3091 {
3092 return( PSA_ERROR_BAD_STATE );
3093 }
3094
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003095 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003096 {
3097 /* Take the unprocessed partial block left over from previous
3098 * update calls, if any, plus the input to this call. Remove
3099 * the last partial block, if any. You get the data that will be
3100 * output in this call. */
3101 expected_output_size =
3102 ( operation->ctx.cipher.unprocessed_len + input_length )
3103 / operation->block_size * operation->block_size;
3104 }
3105 else
3106 {
3107 expected_output_size = input_length;
3108 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003109
Gilles Peskine89d789c2018-06-04 17:17:16 +02003110 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003111 {
3112 status = PSA_ERROR_BUFFER_TOO_SMALL;
3113 goto exit;
3114 }
mohammad160382759612018-03-12 18:16:40 +02003115
mohammad1603503973b2018-03-12 15:59:30 +02003116 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003117 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003118 status = mbedtls_to_psa_error( ret );
3119exit:
3120 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003121 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003122 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003123}
3124
Gilles Peskinee553c652018-06-04 16:22:46 +02003125psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3126 uint8_t *output,
3127 size_t output_size,
3128 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003129{
David Saadab4ecc272019-02-14 13:48:10 +02003130 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003131 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003132 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003133
mohammad1603503973b2018-03-12 15:59:30 +02003134 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003135 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003136 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003137 }
3138 if( operation->iv_required && ! operation->iv_set )
3139 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003140 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003141 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003142
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003143 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003144 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3145 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003146 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003147 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003148 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003149 }
3150
Janos Follath315b51c2018-07-09 16:04:51 +01003151 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3152 temp_output_buffer,
3153 output_length );
3154 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003155 {
Janos Follath315b51c2018-07-09 16:04:51 +01003156 status = mbedtls_to_psa_error( cipher_ret );
3157 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003158 }
Janos Follath315b51c2018-07-09 16:04:51 +01003159
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003160 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003161 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003162 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003163 memcpy( output, temp_output_buffer, *output_length );
3164 else
3165 {
Janos Follath315b51c2018-07-09 16:04:51 +01003166 status = PSA_ERROR_BUFFER_TOO_SMALL;
3167 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003168 }
mohammad1603503973b2018-03-12 15:59:30 +02003169
Gilles Peskine3f108122018-12-07 18:14:53 +01003170 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003171 status = psa_cipher_abort( operation );
3172
3173 return( status );
3174
3175error:
3176
3177 *output_length = 0;
3178
Gilles Peskine3f108122018-12-07 18:14:53 +01003179 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003180 (void) psa_cipher_abort( operation );
3181
3182 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003183}
3184
Gilles Peskinee553c652018-06-04 16:22:46 +02003185psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3186{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003187 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003188 {
3189 /* The object has (apparently) been initialized but it is not
3190 * in use. It's ok to call abort on such an object, and there's
3191 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003192 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003193 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003194
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003195 /* Sanity check (shouldn't happen: operation->alg should
3196 * always have been initialized to a valid value). */
3197 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3198 return( PSA_ERROR_BAD_STATE );
3199
mohammad1603503973b2018-03-12 15:59:30 +02003200 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003201
Moran Peker41deec42018-04-04 15:43:05 +03003202 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003203 operation->key_set = 0;
3204 operation->iv_set = 0;
3205 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003206 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003207 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003208
Moran Peker395db872018-05-31 14:07:14 +03003209 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003210}
3211
Gilles Peskinea0655c32018-04-30 17:06:50 +02003212
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003213
mohammad16038cc1cee2018-03-28 01:21:33 +03003214/****************************************************************/
3215/* Key Policy */
3216/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003217
mohammad160327010052018-07-03 13:16:15 +03003218#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02003219void psa_key_policy_set_usage( psa_key_policy_t *policy,
3220 psa_key_usage_t usage,
3221 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03003222{
mohammad16034eed7572018-03-28 05:14:59 -07003223 policy->usage = usage;
3224 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03003225}
3226
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003227psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003228{
mohammad16036df908f2018-04-02 08:34:15 -07003229 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03003230}
3231
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003232psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003233{
mohammad16036df908f2018-04-02 08:34:15 -07003234 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03003235}
Gilles Peskined6f371b2019-05-10 19:33:38 +02003236
3237void psa_key_policy_set_enrollment_algorithm( psa_key_policy_t *policy,
3238 psa_algorithm_t alg2 )
3239{
3240 policy->alg2 = alg2;
3241}
3242
3243psa_algorithm_t psa_key_policy_get_enrollment_algorithm(
3244 const psa_key_policy_t *policy )
3245{
3246 return( policy->alg2 );
3247}
mohammad160327010052018-07-03 13:16:15 +03003248#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003249
Gilles Peskinec5487a82018-12-03 18:08:14 +01003250psa_status_t psa_set_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003251 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003252{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003253 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003254 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003255
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003256 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003257 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003258
Gilles Peskinec5487a82018-12-03 18:08:14 +01003259 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003260 if( status != PSA_SUCCESS )
3261 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03003262
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003263 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
3264 PSA_KEY_USAGE_ENCRYPT |
3265 PSA_KEY_USAGE_DECRYPT |
3266 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02003267 PSA_KEY_USAGE_VERIFY |
3268 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07003269 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03003270
mohammad16036df908f2018-04-02 08:34:15 -07003271 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003272
3273 return( PSA_SUCCESS );
3274}
3275
Gilles Peskinec5487a82018-12-03 18:08:14 +01003276psa_status_t psa_get_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003277 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003278{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003279 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003280 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003281
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003282 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003283 return( PSA_ERROR_INVALID_ARGUMENT );
3284
Gilles Peskinec5487a82018-12-03 18:08:14 +01003285 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003286 if( status != PSA_SUCCESS )
3287 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003288
mohammad16036df908f2018-04-02 08:34:15 -07003289 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003290
3291 return( PSA_SUCCESS );
3292}
Gilles Peskine20035e32018-02-03 22:44:14 +01003293
Gilles Peskinea0655c32018-04-30 17:06:50 +02003294
3295
mohammad1603804cd712018-03-20 22:44:08 +02003296/****************************************************************/
3297/* Key Lifetime */
3298/****************************************************************/
3299
Gilles Peskinec5487a82018-12-03 18:08:14 +01003300psa_status_t psa_get_key_lifetime( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003301 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003302{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003303 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003304 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003305
Gilles Peskinec5487a82018-12-03 18:08:14 +01003306 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003307 if( status != PSA_SUCCESS )
3308 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003309
mohammad1603804cd712018-03-20 22:44:08 +02003310 *lifetime = slot->lifetime;
3311
3312 return( PSA_SUCCESS );
3313}
3314
Gilles Peskine20035e32018-02-03 22:44:14 +01003315
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003316
mohammad16035955c982018-04-26 00:53:03 +03003317/****************************************************************/
3318/* AEAD */
3319/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003320
Gilles Peskineedf9a652018-08-17 18:11:56 +02003321typedef struct
3322{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003323 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003324 const mbedtls_cipher_info_t *cipher_info;
3325 union
3326 {
3327#if defined(MBEDTLS_CCM_C)
3328 mbedtls_ccm_context ccm;
3329#endif /* MBEDTLS_CCM_C */
3330#if defined(MBEDTLS_GCM_C)
3331 mbedtls_gcm_context gcm;
3332#endif /* MBEDTLS_GCM_C */
3333 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003334 psa_algorithm_t core_alg;
3335 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003336 uint8_t tag_length;
3337} aead_operation_t;
3338
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003339static void psa_aead_abort( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003340{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003341 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003342 {
3343#if defined(MBEDTLS_CCM_C)
3344 case PSA_ALG_CCM:
3345 mbedtls_ccm_free( &operation->ctx.ccm );
3346 break;
3347#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003348#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003349 case PSA_ALG_GCM:
3350 mbedtls_gcm_free( &operation->ctx.gcm );
3351 break;
3352#endif /* MBEDTLS_GCM_C */
3353 }
3354}
3355
3356static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003357 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003358 psa_key_usage_t usage,
3359 psa_algorithm_t alg )
3360{
3361 psa_status_t status;
3362 size_t key_bits;
3363 mbedtls_cipher_id_t cipher_id;
3364
Gilles Peskinec5487a82018-12-03 18:08:14 +01003365 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003366 if( status != PSA_SUCCESS )
3367 return( status );
3368
3369 key_bits = psa_get_key_bits( operation->slot );
3370
3371 operation->cipher_info =
3372 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3373 &cipher_id );
3374 if( operation->cipher_info == NULL )
3375 return( PSA_ERROR_NOT_SUPPORTED );
3376
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003377 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003378 {
3379#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003380 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3381 operation->core_alg = PSA_ALG_CCM;
3382 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003383 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3384 return( PSA_ERROR_INVALID_ARGUMENT );
3385 mbedtls_ccm_init( &operation->ctx.ccm );
3386 status = mbedtls_to_psa_error(
3387 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3388 operation->slot->data.raw.data,
3389 (unsigned int) key_bits ) );
3390 if( status != 0 )
3391 goto cleanup;
3392 break;
3393#endif /* MBEDTLS_CCM_C */
3394
3395#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003396 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3397 operation->core_alg = PSA_ALG_GCM;
3398 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003399 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3400 return( PSA_ERROR_INVALID_ARGUMENT );
3401 mbedtls_gcm_init( &operation->ctx.gcm );
3402 status = mbedtls_to_psa_error(
3403 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3404 operation->slot->data.raw.data,
3405 (unsigned int) key_bits ) );
3406 break;
3407#endif /* MBEDTLS_GCM_C */
3408
3409 default:
3410 return( PSA_ERROR_NOT_SUPPORTED );
3411 }
3412
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003413 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3414 {
3415 status = PSA_ERROR_INVALID_ARGUMENT;
3416 goto cleanup;
3417 }
3418 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3419 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3420 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3421 * In both cases, mbedtls_xxx will validate the tag length below. */
3422
Gilles Peskineedf9a652018-08-17 18:11:56 +02003423 return( PSA_SUCCESS );
3424
3425cleanup:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003426 psa_aead_abort( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003427 return( status );
3428}
3429
Gilles Peskinec5487a82018-12-03 18:08:14 +01003430psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003431 psa_algorithm_t alg,
3432 const uint8_t *nonce,
3433 size_t nonce_length,
3434 const uint8_t *additional_data,
3435 size_t additional_data_length,
3436 const uint8_t *plaintext,
3437 size_t plaintext_length,
3438 uint8_t *ciphertext,
3439 size_t ciphertext_size,
3440 size_t *ciphertext_length )
3441{
mohammad16035955c982018-04-26 00:53:03 +03003442 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003443 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003444 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003445
mohammad1603f08a5502018-06-03 15:05:47 +03003446 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003447
Gilles Peskinec5487a82018-12-03 18:08:14 +01003448 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003449 if( status != PSA_SUCCESS )
3450 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003451
Gilles Peskineedf9a652018-08-17 18:11:56 +02003452 /* For all currently supported modes, the tag is at the end of the
3453 * ciphertext. */
3454 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3455 {
3456 status = PSA_ERROR_BUFFER_TOO_SMALL;
3457 goto exit;
3458 }
3459 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003460
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003461#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003462 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003463 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003464 status = mbedtls_to_psa_error(
3465 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3466 MBEDTLS_GCM_ENCRYPT,
3467 plaintext_length,
3468 nonce, nonce_length,
3469 additional_data, additional_data_length,
3470 plaintext, ciphertext,
3471 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003472 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003473 else
3474#endif /* MBEDTLS_GCM_C */
3475#if defined(MBEDTLS_CCM_C)
3476 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003477 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003478 status = mbedtls_to_psa_error(
3479 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3480 plaintext_length,
3481 nonce, nonce_length,
3482 additional_data,
3483 additional_data_length,
3484 plaintext, ciphertext,
3485 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003486 }
mohammad16035c8845f2018-05-09 05:40:09 -07003487 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003488#endif /* MBEDTLS_CCM_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003489 {
mohammad1603554faad2018-06-03 15:07:38 +03003490 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003491 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003492
Gilles Peskineedf9a652018-08-17 18:11:56 +02003493 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3494 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003495
Gilles Peskineedf9a652018-08-17 18:11:56 +02003496exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003497 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003498 if( status == PSA_SUCCESS )
3499 *ciphertext_length = plaintext_length + operation.tag_length;
3500 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003501}
3502
Gilles Peskineee652a32018-06-01 19:23:52 +02003503/* Locate the tag in a ciphertext buffer containing the encrypted data
3504 * followed by the tag. Return the length of the part preceding the tag in
3505 * *plaintext_length. This is the size of the plaintext in modes where
3506 * the encrypted data has the same size as the plaintext, such as
3507 * CCM and GCM. */
3508static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3509 const uint8_t *ciphertext,
3510 size_t ciphertext_length,
3511 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003512 const uint8_t **p_tag )
3513{
3514 size_t payload_length;
3515 if( tag_length > ciphertext_length )
3516 return( PSA_ERROR_INVALID_ARGUMENT );
3517 payload_length = ciphertext_length - tag_length;
3518 if( payload_length > plaintext_size )
3519 return( PSA_ERROR_BUFFER_TOO_SMALL );
3520 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003521 return( PSA_SUCCESS );
3522}
3523
Gilles Peskinec5487a82018-12-03 18:08:14 +01003524psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003525 psa_algorithm_t alg,
3526 const uint8_t *nonce,
3527 size_t nonce_length,
3528 const uint8_t *additional_data,
3529 size_t additional_data_length,
3530 const uint8_t *ciphertext,
3531 size_t ciphertext_length,
3532 uint8_t *plaintext,
3533 size_t plaintext_size,
3534 size_t *plaintext_length )
3535{
mohammad16035955c982018-04-26 00:53:03 +03003536 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003537 aead_operation_t operation;
3538 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003539
Gilles Peskineee652a32018-06-01 19:23:52 +02003540 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003541
Gilles Peskinec5487a82018-12-03 18:08:14 +01003542 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003543 if( status != PSA_SUCCESS )
3544 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003545
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003546#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003547 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003548 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003549 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003550 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003551 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003552 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003553 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003554
Gilles Peskineedf9a652018-08-17 18:11:56 +02003555 status = mbedtls_to_psa_error(
3556 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3557 ciphertext_length - operation.tag_length,
3558 nonce, nonce_length,
3559 additional_data,
3560 additional_data_length,
3561 tag, operation.tag_length,
3562 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003563 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003564 else
3565#endif /* MBEDTLS_GCM_C */
3566#if defined(MBEDTLS_CCM_C)
3567 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003568 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003569 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003570 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003571 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003572 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003573 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003574
Gilles Peskineedf9a652018-08-17 18:11:56 +02003575 status = mbedtls_to_psa_error(
3576 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3577 ciphertext_length - operation.tag_length,
3578 nonce, nonce_length,
3579 additional_data,
3580 additional_data_length,
3581 ciphertext, plaintext,
3582 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003583 }
mohammad160339574652018-06-01 04:39:53 -07003584 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003585#endif /* MBEDTLS_CCM_C */
mohammad160339574652018-06-01 04:39:53 -07003586 {
mohammad1603554faad2018-06-03 15:07:38 +03003587 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003588 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003589
Gilles Peskineedf9a652018-08-17 18:11:56 +02003590 if( status != PSA_SUCCESS && plaintext_size != 0 )
3591 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003592
Gilles Peskineedf9a652018-08-17 18:11:56 +02003593exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003594 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003595 if( status == PSA_SUCCESS )
3596 *plaintext_length = ciphertext_length - operation.tag_length;
3597 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003598}
3599
Gilles Peskinea0655c32018-04-30 17:06:50 +02003600
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003601
Gilles Peskine20035e32018-02-03 22:44:14 +01003602/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003603/* Generators */
3604/****************************************************************/
3605
3606psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3607{
3608 psa_status_t status = PSA_SUCCESS;
3609 if( generator->alg == 0 )
3610 {
3611 /* The object has (apparently) been initialized but it is not
3612 * in use. It's ok to call abort on such an object, and there's
3613 * nothing to do. */
3614 }
3615 else
Gilles Peskine751d9652018-09-18 12:05:44 +02003616 if( generator->alg == PSA_ALG_SELECT_RAW )
3617 {
3618 if( generator->ctx.buffer.data != NULL )
3619 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003620 mbedtls_platform_zeroize( generator->ctx.buffer.data,
Gilles Peskine751d9652018-09-18 12:05:44 +02003621 generator->ctx.buffer.size );
3622 mbedtls_free( generator->ctx.buffer.data );
3623 }
3624 }
3625 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003626#if defined(MBEDTLS_MD_C)
3627 if( PSA_ALG_IS_HKDF( generator->alg ) )
3628 {
3629 mbedtls_free( generator->ctx.hkdf.info );
3630 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3631 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003632 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3633 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
3634 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003635 {
3636 if( generator->ctx.tls12_prf.key != NULL )
3637 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003638 mbedtls_platform_zeroize( generator->ctx.tls12_prf.key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003639 generator->ctx.tls12_prf.key_len );
3640 mbedtls_free( generator->ctx.tls12_prf.key );
3641 }
Hanno Becker580fba12018-11-13 20:50:45 +00003642
3643 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
3644 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003645 mbedtls_platform_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003646 generator->ctx.tls12_prf.Ai_with_seed_len );
3647 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
3648 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003649 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003650 else
3651#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003652 {
3653 status = PSA_ERROR_BAD_STATE;
3654 }
3655 memset( generator, 0, sizeof( *generator ) );
3656 return( status );
3657}
3658
3659
3660psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3661 size_t *capacity)
3662{
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003663 if( generator->alg == 0 )
3664 {
3665 /* This is a blank generator. */
3666 return PSA_ERROR_BAD_STATE;
3667 }
3668
Gilles Peskineeab56e42018-07-12 17:12:33 +02003669 *capacity = generator->capacity;
3670 return( PSA_SUCCESS );
3671}
3672
Gilles Peskinebef7f142018-07-12 17:22:21 +02003673#if defined(MBEDTLS_MD_C)
3674/* Read some bytes from an HKDF-based generator. This performs a chunk
3675 * of the expand phase of the HKDF algorithm. */
3676static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3677 psa_algorithm_t hash_alg,
3678 uint8_t *output,
3679 size_t output_length )
3680{
3681 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3682 psa_status_t status;
3683
3684 while( output_length != 0 )
3685 {
3686 /* Copy what remains of the current block */
3687 uint8_t n = hash_length - hkdf->offset_in_block;
3688 if( n > output_length )
3689 n = (uint8_t) output_length;
3690 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3691 output += n;
3692 output_length -= n;
3693 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003694 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003695 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003696 /* We can't be wanting more output after block 0xff, otherwise
3697 * the capacity check in psa_generator_read() would have
3698 * prevented this call. It could happen only if the generator
3699 * object was corrupted or if this function is called directly
3700 * inside the library. */
3701 if( hkdf->block_number == 0xff )
3702 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003703
3704 /* We need a new block */
3705 ++hkdf->block_number;
3706 hkdf->offset_in_block = 0;
3707 status = psa_hmac_setup_internal( &hkdf->hmac,
3708 hkdf->prk, hash_length,
3709 hash_alg );
3710 if( status != PSA_SUCCESS )
3711 return( status );
3712 if( hkdf->block_number != 1 )
3713 {
3714 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3715 hkdf->output_block,
3716 hash_length );
3717 if( status != PSA_SUCCESS )
3718 return( status );
3719 }
3720 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3721 hkdf->info,
3722 hkdf->info_length );
3723 if( status != PSA_SUCCESS )
3724 return( status );
3725 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3726 &hkdf->block_number, 1 );
3727 if( status != PSA_SUCCESS )
3728 return( status );
3729 status = psa_hmac_finish_internal( &hkdf->hmac,
3730 hkdf->output_block,
3731 sizeof( hkdf->output_block ) );
3732 if( status != PSA_SUCCESS )
3733 return( status );
3734 }
3735
3736 return( PSA_SUCCESS );
3737}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003738
3739static psa_status_t psa_generator_tls12_prf_generate_next_block(
3740 psa_tls12_prf_generator_t *tls12_prf,
3741 psa_algorithm_t alg )
3742{
3743 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3744 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3745 psa_hmac_internal_data hmac;
3746 psa_status_t status, cleanup_status;
3747
Hanno Becker3b339e22018-11-13 20:56:14 +00003748 unsigned char *Ai;
3749 size_t Ai_len;
3750
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003751 /* We can't be wanting more output after block 0xff, otherwise
3752 * the capacity check in psa_generator_read() would have
3753 * prevented this call. It could happen only if the generator
3754 * object was corrupted or if this function is called directly
3755 * inside the library. */
3756 if( tls12_prf->block_number == 0xff )
3757 return( PSA_ERROR_BAD_STATE );
3758
3759 /* We need a new block */
3760 ++tls12_prf->block_number;
3761 tls12_prf->offset_in_block = 0;
3762
3763 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
3764 *
3765 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
3766 *
3767 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
3768 * HMAC_hash(secret, A(2) + seed) +
3769 * HMAC_hash(secret, A(3) + seed) + ...
3770 *
3771 * A(0) = seed
3772 * A(i) = HMAC_hash( secret, A(i-1) )
3773 *
3774 * The `psa_tls12_prf_generator` structures saves the block
3775 * `HMAC_hash(secret, A(i) + seed)` from which the output
3776 * is currently extracted as `output_block`, while
3777 * `A(i) + seed` is stored in `Ai_with_seed`.
3778 *
3779 * Generating a new block means recalculating `Ai_with_seed`
3780 * from the A(i)-part of it, and afterwards recalculating
3781 * `output_block`.
3782 *
3783 * A(0) is computed at setup time.
3784 *
3785 */
3786
3787 psa_hmac_init_internal( &hmac );
3788
3789 /* We must distinguish the calculation of A(1) from those
3790 * of A(2) and higher, because A(0)=seed has a different
3791 * length than the other A(i). */
3792 if( tls12_prf->block_number == 1 )
3793 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003794 Ai = tls12_prf->Ai_with_seed + hash_length;
3795 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003796 }
3797 else
3798 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003799 Ai = tls12_prf->Ai_with_seed;
3800 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003801 }
3802
Hanno Becker3b339e22018-11-13 20:56:14 +00003803 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
3804 status = psa_hmac_setup_internal( &hmac,
3805 tls12_prf->key,
3806 tls12_prf->key_len,
3807 hash_alg );
3808 if( status != PSA_SUCCESS )
3809 goto cleanup;
3810
3811 status = psa_hash_update( &hmac.hash_ctx,
3812 Ai, Ai_len );
3813 if( status != PSA_SUCCESS )
3814 goto cleanup;
3815
3816 status = psa_hmac_finish_internal( &hmac,
3817 tls12_prf->Ai_with_seed,
3818 hash_length );
3819 if( status != PSA_SUCCESS )
3820 goto cleanup;
3821
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003822 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
3823 status = psa_hmac_setup_internal( &hmac,
3824 tls12_prf->key,
3825 tls12_prf->key_len,
3826 hash_alg );
3827 if( status != PSA_SUCCESS )
3828 goto cleanup;
3829
3830 status = psa_hash_update( &hmac.hash_ctx,
3831 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003832 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003833 if( status != PSA_SUCCESS )
3834 goto cleanup;
3835
3836 status = psa_hmac_finish_internal( &hmac,
3837 tls12_prf->output_block,
3838 hash_length );
3839 if( status != PSA_SUCCESS )
3840 goto cleanup;
3841
3842cleanup:
3843
3844 cleanup_status = psa_hmac_abort_internal( &hmac );
3845 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
3846 status = cleanup_status;
3847
3848 return( status );
3849}
3850
3851/* Read some bytes from an TLS-1.2-PRF-based generator.
3852 * See Section 5 of RFC 5246. */
3853static psa_status_t psa_generator_tls12_prf_read(
3854 psa_tls12_prf_generator_t *tls12_prf,
3855 psa_algorithm_t alg,
3856 uint8_t *output,
3857 size_t output_length )
3858{
3859 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3860 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3861 psa_status_t status;
3862
3863 while( output_length != 0 )
3864 {
3865 /* Copy what remains of the current block */
3866 uint8_t n = hash_length - tls12_prf->offset_in_block;
3867
3868 /* Check if we have fully processed the current block. */
3869 if( n == 0 )
3870 {
3871 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
3872 alg );
3873 if( status != PSA_SUCCESS )
3874 return( status );
3875
3876 continue;
3877 }
3878
3879 if( n > output_length )
3880 n = (uint8_t) output_length;
3881 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
3882 n );
3883 output += n;
3884 output_length -= n;
3885 tls12_prf->offset_in_block += n;
3886 }
3887
3888 return( PSA_SUCCESS );
3889}
Gilles Peskinebef7f142018-07-12 17:22:21 +02003890#endif /* MBEDTLS_MD_C */
3891
Gilles Peskineeab56e42018-07-12 17:12:33 +02003892psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3893 uint8_t *output,
3894 size_t output_length )
3895{
3896 psa_status_t status;
3897
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003898 if( generator->alg == 0 )
3899 {
3900 /* This is a blank generator. */
3901 return PSA_ERROR_BAD_STATE;
3902 }
3903
Gilles Peskineeab56e42018-07-12 17:12:33 +02003904 if( output_length > generator->capacity )
3905 {
3906 generator->capacity = 0;
3907 /* Go through the error path to wipe all confidential data now
3908 * that the generator object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02003909 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003910 goto exit;
3911 }
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003912 if( output_length == 0 && generator->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003913 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003914 /* Edge case: this is a finished generator, and 0 bytes
3915 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02003916 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3917 * INSUFFICIENT_CAPACITY, which is right for a finished
3918 * generator, for consistency with the case when
3919 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02003920 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003921 }
3922 generator->capacity -= output_length;
3923
Gilles Peskine751d9652018-09-18 12:05:44 +02003924 if( generator->alg == PSA_ALG_SELECT_RAW )
3925 {
Gilles Peskine211a4362018-10-25 22:22:31 +02003926 /* Initially, the capacity of a selection generator is always
3927 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
3928 * abbreviated in this comment as `size`. When the remaining
3929 * capacity is `c`, the next bytes to serve start `c` bytes
3930 * from the end of the buffer, i.e. `size - c` from the
3931 * beginning of the buffer. Since `generator->capacity` was just
3932 * decremented above, we need to serve the bytes from
3933 * `size - generator->capacity - output_length` to
3934 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02003935 size_t offset =
3936 generator->ctx.buffer.size - generator->capacity - output_length;
3937 memcpy( output, generator->ctx.buffer.data + offset, output_length );
3938 status = PSA_SUCCESS;
3939 }
3940 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003941#if defined(MBEDTLS_MD_C)
3942 if( PSA_ALG_IS_HKDF( generator->alg ) )
3943 {
3944 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3945 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3946 output, output_length );
3947 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003948 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3949 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003950 {
3951 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
3952 generator->alg, output,
3953 output_length );
3954 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003955 else
3956#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003957 {
3958 return( PSA_ERROR_BAD_STATE );
3959 }
3960
3961exit:
3962 if( status != PSA_SUCCESS )
3963 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003964 /* Preserve the algorithm upon errors, but clear all sensitive state.
3965 * This allows us to differentiate between exhausted generators and
3966 * blank generators, so we can return PSA_ERROR_BAD_STATE on blank
3967 * generators. */
3968 psa_algorithm_t alg = generator->alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003969 psa_generator_abort( generator );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003970 generator->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003971 memset( output, '!', output_length );
3972 }
3973 return( status );
3974}
3975
Gilles Peskine08542d82018-07-19 17:05:42 +02003976#if defined(MBEDTLS_DES_C)
3977static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3978{
3979 if( data_size >= 8 )
3980 mbedtls_des_key_set_parity( data );
3981 if( data_size >= 16 )
3982 mbedtls_des_key_set_parity( data + 8 );
3983 if( data_size >= 24 )
3984 mbedtls_des_key_set_parity( data + 16 );
3985}
3986#endif /* MBEDTLS_DES_C */
3987
Gilles Peskinec5487a82018-12-03 18:08:14 +01003988psa_status_t psa_generator_import_key( psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003989 psa_key_type_t type,
3990 size_t bits,
3991 psa_crypto_generator_t *generator )
3992{
3993 uint8_t *data = NULL;
3994 size_t bytes = PSA_BITS_TO_BYTES( bits );
3995 psa_status_t status;
3996
3997 if( ! key_type_is_raw_bytes( type ) )
3998 return( PSA_ERROR_INVALID_ARGUMENT );
3999 if( bits % 8 != 0 )
4000 return( PSA_ERROR_INVALID_ARGUMENT );
4001 data = mbedtls_calloc( 1, bytes );
4002 if( data == NULL )
4003 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4004
4005 status = psa_generator_read( generator, data, bytes );
4006 if( status != PSA_SUCCESS )
4007 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004008#if defined(MBEDTLS_DES_C)
4009 if( type == PSA_KEY_TYPE_DES )
4010 psa_des_set_key_parity( data, bytes );
4011#endif /* MBEDTLS_DES_C */
Gilles Peskinec5487a82018-12-03 18:08:14 +01004012 status = psa_import_key( handle, type, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004013
4014exit:
4015 mbedtls_free( data );
4016 return( status );
4017}
4018
4019
4020
4021/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004022/* Key derivation */
4023/****************************************************************/
4024
Gilles Peskinea05219c2018-11-16 16:02:56 +01004025#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02004026/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004027 * of the HKDF algorithm.
4028 *
4029 * Note that if this function fails, you must call psa_generator_abort()
4030 * to potentially free embedded data structures and wipe confidential data.
4031 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004032static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004033 const uint8_t *secret,
4034 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004035 psa_algorithm_t hash_alg,
4036 const uint8_t *salt,
4037 size_t salt_length,
4038 const uint8_t *label,
4039 size_t label_length )
4040{
4041 psa_status_t status;
4042 status = psa_hmac_setup_internal( &hkdf->hmac,
4043 salt, salt_length,
Gilles Peskine00709fa2018-08-22 18:25:41 +02004044 PSA_ALG_HMAC_GET_HASH( hash_alg ) );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004045 if( status != PSA_SUCCESS )
4046 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004047 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004048 if( status != PSA_SUCCESS )
4049 return( status );
4050 status = psa_hmac_finish_internal( &hkdf->hmac,
4051 hkdf->prk,
4052 sizeof( hkdf->prk ) );
4053 if( status != PSA_SUCCESS )
4054 return( status );
4055 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4056 hkdf->block_number = 0;
4057 hkdf->info_length = label_length;
4058 if( label_length != 0 )
4059 {
4060 hkdf->info = mbedtls_calloc( 1, label_length );
4061 if( hkdf->info == NULL )
4062 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4063 memcpy( hkdf->info, label, label_length );
4064 }
4065 return( PSA_SUCCESS );
4066}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004067#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004068
Gilles Peskinea05219c2018-11-16 16:02:56 +01004069#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01004070/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
4071 *
4072 * Note that if this function fails, you must call psa_generator_abort()
4073 * to potentially free embedded data structures and wipe confidential data.
4074 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004075static psa_status_t psa_generator_tls12_prf_setup(
4076 psa_tls12_prf_generator_t *tls12_prf,
4077 const unsigned char *key,
4078 size_t key_len,
4079 psa_algorithm_t hash_alg,
4080 const uint8_t *salt,
4081 size_t salt_length,
4082 const uint8_t *label,
4083 size_t label_length )
4084{
4085 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004086 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4087 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004088
4089 tls12_prf->key = mbedtls_calloc( 1, key_len );
4090 if( tls12_prf->key == NULL )
4091 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4092 tls12_prf->key_len = key_len;
4093 memcpy( tls12_prf->key, key, key_len );
4094
Hanno Becker580fba12018-11-13 20:50:45 +00004095 overflow = ( salt_length + label_length < salt_length ) ||
4096 ( salt_length + label_length + hash_length < hash_length );
4097 if( overflow )
4098 return( PSA_ERROR_INVALID_ARGUMENT );
4099
4100 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4101 if( tls12_prf->Ai_with_seed == NULL )
4102 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4103 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4104
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004105 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4106 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004107 if( label_length != 0 )
4108 {
4109 memcpy( tls12_prf->Ai_with_seed + hash_length,
4110 label, label_length );
4111 }
4112
4113 if( salt_length != 0 )
4114 {
4115 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4116 salt, salt_length );
4117 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004118
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004119 /* The first block gets generated when
4120 * psa_generator_read() is called. */
4121 tls12_prf->block_number = 0;
4122 tls12_prf->offset_in_block = hash_length;
4123
4124 return( PSA_SUCCESS );
4125}
Hanno Becker1aaedc02018-11-16 11:35:34 +00004126
4127/* Set up a TLS-1.2-PSK-to-MS-based generator. */
4128static psa_status_t psa_generator_tls12_psk_to_ms_setup(
4129 psa_tls12_prf_generator_t *tls12_prf,
4130 const unsigned char *psk,
4131 size_t psk_len,
4132 psa_algorithm_t hash_alg,
4133 const uint8_t *salt,
4134 size_t salt_length,
4135 const uint8_t *label,
4136 size_t label_length )
4137{
4138 psa_status_t status;
4139 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4140
4141 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4142 return( PSA_ERROR_INVALID_ARGUMENT );
4143
4144 /* Quoting RFC 4279, Section 2:
4145 *
4146 * The premaster secret is formed as follows: if the PSK is N octets
4147 * long, concatenate a uint16 with the value N, N zero octets, a second
4148 * uint16 with the value N, and the PSK itself.
4149 */
4150
4151 pms[0] = ( psk_len >> 8 ) & 0xff;
4152 pms[1] = ( psk_len >> 0 ) & 0xff;
4153 memset( pms + 2, 0, psk_len );
4154 pms[2 + psk_len + 0] = pms[0];
4155 pms[2 + psk_len + 1] = pms[1];
4156 memcpy( pms + 4 + psk_len, psk, psk_len );
4157
4158 status = psa_generator_tls12_prf_setup( tls12_prf,
4159 pms, 4 + 2 * psk_len,
4160 hash_alg,
4161 salt, salt_length,
4162 label, label_length );
4163
Gilles Peskine3f108122018-12-07 18:14:53 +01004164 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004165 return( status );
4166}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004167#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004168
Gilles Peskine346797d2018-11-16 16:05:06 +01004169/* Note that if this function fails, you must call psa_generator_abort()
4170 * to potentially free embedded data structures and wipe confidential data.
4171 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004172static psa_status_t psa_key_derivation_internal(
4173 psa_crypto_generator_t *generator,
4174 const uint8_t *secret, size_t secret_length,
4175 psa_algorithm_t alg,
4176 const uint8_t *salt, size_t salt_length,
4177 const uint8_t *label, size_t label_length,
4178 size_t capacity )
4179{
4180 psa_status_t status;
4181 size_t max_capacity;
4182
4183 /* Set generator->alg even on failure so that abort knows what to do. */
4184 generator->alg = alg;
4185
Gilles Peskine751d9652018-09-18 12:05:44 +02004186 if( alg == PSA_ALG_SELECT_RAW )
4187 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01004188 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02004189 if( salt_length != 0 )
4190 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01004191 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02004192 if( label_length != 0 )
4193 return( PSA_ERROR_INVALID_ARGUMENT );
4194 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
4195 if( generator->ctx.buffer.data == NULL )
4196 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4197 memcpy( generator->ctx.buffer.data, secret, secret_length );
4198 generator->ctx.buffer.size = secret_length;
4199 max_capacity = secret_length;
4200 status = PSA_SUCCESS;
4201 }
4202 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004203#if defined(MBEDTLS_MD_C)
4204 if( PSA_ALG_IS_HKDF( alg ) )
4205 {
4206 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4207 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4208 if( hash_size == 0 )
4209 return( PSA_ERROR_NOT_SUPPORTED );
4210 max_capacity = 255 * hash_size;
4211 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
4212 secret, secret_length,
4213 hash_alg,
4214 salt, salt_length,
4215 label, label_length );
4216 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004217 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4218 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4219 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004220 {
4221 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4222 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4223
4224 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4225 if( hash_alg != PSA_ALG_SHA_256 &&
4226 hash_alg != PSA_ALG_SHA_384 )
4227 {
4228 return( PSA_ERROR_NOT_SUPPORTED );
4229 }
4230
4231 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004232
4233 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4234 {
4235 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
4236 secret, secret_length,
4237 hash_alg, salt, salt_length,
4238 label, label_length );
4239 }
4240 else
4241 {
4242 status = psa_generator_tls12_psk_to_ms_setup(
4243 &generator->ctx.tls12_prf,
4244 secret, secret_length,
4245 hash_alg, salt, salt_length,
4246 label, label_length );
4247 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004248 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004249 else
4250#endif
4251 {
4252 return( PSA_ERROR_NOT_SUPPORTED );
4253 }
4254
4255 if( status != PSA_SUCCESS )
4256 return( status );
4257
4258 if( capacity <= max_capacity )
4259 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02004260 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
4261 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004262 else
4263 return( PSA_ERROR_INVALID_ARGUMENT );
4264
4265 return( PSA_SUCCESS );
4266}
4267
Gilles Peskineea0fb492018-07-12 17:17:20 +02004268psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004269 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004270 psa_algorithm_t alg,
4271 const uint8_t *salt,
4272 size_t salt_length,
4273 const uint8_t *label,
4274 size_t label_length,
4275 size_t capacity )
4276{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004277 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004278 psa_status_t status;
4279
4280 if( generator->alg != 0 )
4281 return( PSA_ERROR_BAD_STATE );
4282
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004283 /* Make sure that alg is a key derivation algorithm. This prevents
4284 * key selection algorithms, which psa_key_derivation_internal
4285 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004286 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4287 return( PSA_ERROR_INVALID_ARGUMENT );
4288
Gilles Peskinec5487a82018-12-03 18:08:14 +01004289 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004290 if( status != PSA_SUCCESS )
4291 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004292
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004293 if( slot->type != PSA_KEY_TYPE_DERIVE )
4294 return( PSA_ERROR_INVALID_ARGUMENT );
4295
4296 status = psa_key_derivation_internal( generator,
4297 slot->data.raw.data,
4298 slot->data.raw.bytes,
4299 alg,
4300 salt, salt_length,
4301 label, label_length,
4302 capacity );
4303 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004304 psa_generator_abort( generator );
4305 return( status );
4306}
4307
4308
4309
4310/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004311/* Key agreement */
4312/****************************************************************/
4313
Gilles Peskinea05219c2018-11-16 16:02:56 +01004314#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004315static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4316 size_t peer_key_length,
4317 const mbedtls_ecp_keypair *our_key,
4318 uint8_t *shared_secret,
4319 size_t shared_secret_size,
4320 size_t *shared_secret_length )
4321{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004322 mbedtls_ecp_keypair *their_key = NULL;
4323 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00004324 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004325 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004326
Jaeden Ameroccdce902019-01-10 11:42:27 +00004327 status = psa_import_ec_public_key(
4328 mbedtls_ecc_group_to_psa( our_key->grp.id ),
4329 peer_key, peer_key_length,
4330 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00004331 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004332 goto exit;
4333
Jaeden Amero97271b32019-01-10 19:38:51 +00004334 status = mbedtls_to_psa_error(
4335 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
4336 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004337 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00004338 status = mbedtls_to_psa_error(
4339 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
4340 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004341 goto exit;
4342
Jaeden Amero97271b32019-01-10 19:38:51 +00004343 status = mbedtls_to_psa_error(
4344 mbedtls_ecdh_calc_secret( &ecdh,
4345 shared_secret_length,
4346 shared_secret, shared_secret_size,
4347 mbedtls_ctr_drbg_random,
4348 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004349
4350exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004351 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00004352 mbedtls_ecp_keypair_free( their_key );
4353 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00004354 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004355}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004356#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004357
Gilles Peskine01d718c2018-09-18 12:01:02 +02004358#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4359
Gilles Peskine346797d2018-11-16 16:05:06 +01004360/* Note that if this function fails, you must call psa_generator_abort()
4361 * to potentially free embedded data structures and wipe confidential data.
4362 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004363static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
Gilles Peskine2f060a82018-12-04 17:12:32 +01004364 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004365 const uint8_t *peer_key,
4366 size_t peer_key_length,
4367 psa_algorithm_t alg )
4368{
4369 psa_status_t status;
4370 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4371 size_t shared_secret_length = 0;
4372
4373 /* Step 1: run the secret agreement algorithm to generate the shared
4374 * secret. */
4375 switch( PSA_ALG_KEY_AGREEMENT_GET_BASE( alg ) )
4376 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004377#if defined(MBEDTLS_ECDH_C)
4378 case PSA_ALG_ECDH_BASE:
4379 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
4380 return( PSA_ERROR_INVALID_ARGUMENT );
4381 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
4382 private_key->data.ecp,
4383 shared_secret,
4384 sizeof( shared_secret ),
4385 &shared_secret_length );
4386 break;
4387#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004388 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01004389 (void) private_key;
4390 (void) peer_key;
4391 (void) peer_key_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004392 return( PSA_ERROR_NOT_SUPPORTED );
4393 }
4394 if( status != PSA_SUCCESS )
4395 goto exit;
4396
4397 /* Step 2: set up the key derivation to generate key material from
4398 * the shared secret. */
4399 status = psa_key_derivation_internal( generator,
4400 shared_secret, shared_secret_length,
4401 PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ),
4402 NULL, 0, NULL, 0,
4403 PSA_GENERATOR_UNBRIDLED_CAPACITY );
4404exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01004405 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004406 return( status );
4407}
4408
4409psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004410 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004411 const uint8_t *peer_key,
4412 size_t peer_key_length,
4413 psa_algorithm_t alg )
4414{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004415 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004416 psa_status_t status;
4417 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
4418 return( PSA_ERROR_INVALID_ARGUMENT );
4419 status = psa_get_key_from_slot( private_key, &slot,
4420 PSA_KEY_USAGE_DERIVE, alg );
4421 if( status != PSA_SUCCESS )
4422 return( status );
Gilles Peskine346797d2018-11-16 16:05:06 +01004423 status = psa_key_agreement_internal( generator,
4424 slot,
4425 peer_key, peer_key_length,
4426 alg );
4427 if( status != PSA_SUCCESS )
4428 psa_generator_abort( generator );
4429 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004430}
4431
4432
4433
4434/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004435/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02004436/****************************************************************/
4437
4438psa_status_t psa_generate_random( uint8_t *output,
4439 size_t output_size )
4440{
itayzafrir0adf0fc2018-09-06 16:24:41 +03004441 int ret;
4442 GUARD_MODULE_INITIALIZED;
4443
4444 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02004445 return( mbedtls_to_psa_error( ret ) );
4446}
4447
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01004448#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
4449#include "mbedtls/entropy_poll.h"
4450
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004451psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
4452 size_t seed_size )
4453{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004454 if( global_data.initialized )
4455 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02004456
4457 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
4458 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
4459 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
4460 return( PSA_ERROR_INVALID_ARGUMENT );
4461
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01004462 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004463}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01004464#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004465
Gilles Peskinec5487a82018-12-03 18:08:14 +01004466psa_status_t psa_generate_key( psa_key_handle_t handle,
Gilles Peskine05d69892018-06-19 22:00:52 +02004467 psa_key_type_t type,
4468 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02004469 const void *extra,
4470 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02004471{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004472 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004473 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004474
Gilles Peskine53d991e2018-07-12 01:14:59 +02004475 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004476 return( PSA_ERROR_INVALID_ARGUMENT );
4477
Gilles Peskinec5487a82018-12-03 18:08:14 +01004478 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004479 if( status != PSA_SUCCESS )
4480 return( status );
4481
Gilles Peskine48c0ea12018-06-21 14:15:31 +02004482 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004483 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004484 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004485 if( status != PSA_SUCCESS )
4486 return( status );
4487 status = psa_generate_random( slot->data.raw.data,
4488 slot->data.raw.bytes );
4489 if( status != PSA_SUCCESS )
4490 {
4491 mbedtls_free( slot->data.raw.data );
4492 return( status );
4493 }
4494#if defined(MBEDTLS_DES_C)
4495 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004496 psa_des_set_key_parity( slot->data.raw.data,
4497 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004498#endif /* MBEDTLS_DES_C */
4499 }
4500 else
4501
4502#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
4503 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
4504 {
4505 mbedtls_rsa_context *rsa;
4506 int ret;
4507 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02004508 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
4509 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01004510 /* Accept only byte-aligned keys, for the same reasons as
4511 * in psa_import_rsa_key(). */
4512 if( bits % 8 != 0 )
4513 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02004514 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004515 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02004516 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004517 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004518 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02004519#if INT_MAX < 0xffffffff
4520 /* Check that the uint32_t value passed by the caller fits
4521 * in the range supported by this implementation. */
4522 if( p->e > INT_MAX )
4523 return( PSA_ERROR_NOT_SUPPORTED );
4524#endif
4525 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004526 }
4527 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
4528 if( rsa == NULL )
4529 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4530 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
4531 ret = mbedtls_rsa_gen_key( rsa,
4532 mbedtls_ctr_drbg_random,
4533 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004534 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02004535 exponent );
4536 if( ret != 0 )
4537 {
4538 mbedtls_rsa_free( rsa );
4539 mbedtls_free( rsa );
4540 return( mbedtls_to_psa_error( ret ) );
4541 }
4542 slot->data.rsa = rsa;
4543 }
4544 else
4545#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
4546
4547#if defined(MBEDTLS_ECP_C)
4548 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
4549 {
4550 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
4551 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
4552 const mbedtls_ecp_curve_info *curve_info =
4553 mbedtls_ecp_curve_info_from_grp_id( grp_id );
4554 mbedtls_ecp_keypair *ecp;
4555 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004556 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004557 return( PSA_ERROR_NOT_SUPPORTED );
4558 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
4559 return( PSA_ERROR_NOT_SUPPORTED );
4560 if( curve_info->bit_size != bits )
4561 return( PSA_ERROR_INVALID_ARGUMENT );
4562 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
4563 if( ecp == NULL )
4564 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4565 mbedtls_ecp_keypair_init( ecp );
4566 ret = mbedtls_ecp_gen_key( grp_id, ecp,
4567 mbedtls_ctr_drbg_random,
4568 &global_data.ctr_drbg );
4569 if( ret != 0 )
4570 {
4571 mbedtls_ecp_keypair_free( ecp );
4572 mbedtls_free( ecp );
4573 return( mbedtls_to_psa_error( ret ) );
4574 }
4575 slot->data.ecp = ecp;
4576 }
4577 else
4578#endif /* MBEDTLS_ECP_C */
4579
4580 return( PSA_ERROR_NOT_SUPPORTED );
4581
4582 slot->type = type;
Darryl Green0c6575a2018-11-07 16:05:30 +00004583
4584#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
4585 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
4586 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01004587 return( psa_save_generated_persistent_key( slot, bits ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004588 }
4589#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
4590
4591 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02004592}
4593
4594
4595/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01004596/* Module setup */
4597/****************************************************************/
4598
Gilles Peskine5e769522018-11-20 21:59:56 +01004599psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
4600 void (* entropy_init )( mbedtls_entropy_context *ctx ),
4601 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
4602{
4603 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4604 return( PSA_ERROR_BAD_STATE );
4605 global_data.entropy_init = entropy_init;
4606 global_data.entropy_free = entropy_free;
4607 return( PSA_SUCCESS );
4608}
4609
Gilles Peskinee59236f2018-01-27 23:32:46 +01004610void mbedtls_psa_crypto_free( void )
4611{
Gilles Peskine66fb1262018-12-10 16:29:04 +01004612 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004613 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4614 {
4615 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01004616 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004617 }
4618 /* Wipe all remaining data, including configuration.
4619 * In particular, this sets all state indicator to the value
4620 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01004621 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004622}
4623
4624psa_status_t psa_crypto_init( void )
4625{
Gilles Peskine66fb1262018-12-10 16:29:04 +01004626 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004627 const unsigned char drbg_seed[] = "PSA";
4628
Gilles Peskinec6b69072018-11-20 21:42:52 +01004629 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004630 if( global_data.initialized != 0 )
4631 return( PSA_SUCCESS );
4632
Gilles Peskine5e769522018-11-20 21:59:56 +01004633 /* Set default configuration if
4634 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
4635 if( global_data.entropy_init == NULL )
4636 global_data.entropy_init = mbedtls_entropy_init;
4637 if( global_data.entropy_free == NULL )
4638 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004639
Gilles Peskinec6b69072018-11-20 21:42:52 +01004640 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01004641 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01004642#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
4643 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
4644 /* The PSA entropy injection feature depends on using NV seed as an entropy
4645 * source. Add NV seed as an entropy source for PSA entropy injection. */
4646 mbedtls_entropy_add_source( &global_data.entropy,
4647 mbedtls_nv_seed_poll, NULL,
4648 MBEDTLS_ENTROPY_BLOCK_SIZE,
4649 MBEDTLS_ENTROPY_SOURCE_STRONG );
4650#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01004651 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004652 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01004653 status = mbedtls_to_psa_error(
4654 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
4655 mbedtls_entropy_func,
4656 &global_data.entropy,
4657 drbg_seed, sizeof( drbg_seed ) - 1 ) );
4658 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004659 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004660 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004661
Gilles Peskine66fb1262018-12-10 16:29:04 +01004662 status = psa_initialize_key_slots( );
4663 if( status != PSA_SUCCESS )
4664 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004665
4666 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01004667 global_data.initialized = 1;
4668
Gilles Peskinee59236f2018-01-27 23:32:46 +01004669exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01004670 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004671 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01004672 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004673}
4674
4675#endif /* MBEDTLS_PSA_CRYPTO_C */