blob: 0ac1c2707eda504c986c2156d587b44c544e131d [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)
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +030029/*
30 * In case MBEDTLS_PSA_CRYPTO_SPM is defined the code is built for SPM (Secure
31 * Partition Manager) integration which separate the code into two parts
32 * NSPE (Non-Secure Process Environment) and SPE (Secure Process Environment).
33 * In this mode an additional header file should be included.
34 */
mohammad160327010052018-07-03 13:16:15 +030035#if defined(MBEDTLS_PSA_CRYPTO_SPM)
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +030036/*
37 * PSA_CRYPTO_SECURE means that this file is compiled to the SPE side.
38 * some headers will be affected by this flag.
39 */
mohammad160327010052018-07-03 13:16:15 +030040#define PSA_CRYPTO_SECURE 1
41#include "crypto_spe.h"
42#endif
43
Gilles Peskinee59236f2018-01-27 23:32:46 +010044#include "psa/crypto.h"
45
Gilles Peskine039b90c2018-12-07 18:24:41 +010046#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010047#include "psa_crypto_invasive.h"
Gilles Peskine961849f2018-11-30 18:54:54 +010048#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010049/* Include internal declarations that are useful for implementing persistently
50 * stored keys. */
51#include "psa_crypto_storage.h"
52
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010053#include <stdlib.h>
54#include <string.h>
55#if defined(MBEDTLS_PLATFORM_C)
56#include "mbedtls/platform.h"
57#else
58#define mbedtls_calloc calloc
59#define mbedtls_free free
60#endif
61
Gilles Peskinea5905292018-02-07 20:59:33 +010062#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020063#include "mbedtls/asn1.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020064#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010065#include "mbedtls/blowfish.h"
66#include "mbedtls/camellia.h"
67#include "mbedtls/cipher.h"
68#include "mbedtls/ccm.h"
69#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010070#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010071#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020072#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010073#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010074#include "mbedtls/entropy.h"
Netanel Gonen2bcd3122018-11-19 11:53:02 +020075#include "mbedtls/entropy_poll.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010076#include "mbedtls/error.h"
77#include "mbedtls/gcm.h"
78#include "mbedtls/md2.h"
79#include "mbedtls/md4.h"
80#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010081#include "mbedtls/md.h"
82#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010083#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010084#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010085#include "mbedtls/platform_util.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010086#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010087#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010088#include "mbedtls/sha1.h"
89#include "mbedtls/sha256.h"
90#include "mbedtls/sha512.h"
91#include "mbedtls/xtea.h"
92
Netanel Gonen2bcd3122018-11-19 11:53:02 +020093#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
94#include "psa_prot_internal_storage.h"
95#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +010096
Gilles Peskine996deb12018-08-01 15:45:45 +020097#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
98
Gilles Peskine9ef733f2018-02-07 21:05:37 +010099/* constant-time buffer comparison */
100static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
101{
102 size_t i;
103 unsigned char diff = 0;
104
105 for( i = 0; i < n; i++ )
106 diff |= a[i] ^ b[i];
107
108 return( diff );
109}
110
111
112
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100113/****************************************************************/
114/* Global data, support functions and library management */
115/****************************************************************/
116
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200117static int key_type_is_raw_bytes( psa_key_type_t type )
118{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200119 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200120}
121
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100122/* Values for psa_global_data_t::rng_state */
123#define RNG_NOT_INITIALIZED 0
124#define RNG_INITIALIZED 1
125#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100126
Gilles Peskine2d277862018-06-18 15:41:12 +0200127typedef struct
128{
Gilles Peskine5e769522018-11-20 21:59:56 +0100129 void (* entropy_init )( mbedtls_entropy_context *ctx );
130 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100131 mbedtls_entropy_context entropy;
132 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100133 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100134 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100135} psa_global_data_t;
136
137static psa_global_data_t global_data;
138
itayzafrir0adf0fc2018-09-06 16:24:41 +0300139#define GUARD_MODULE_INITIALIZED \
140 if( global_data.initialized == 0 ) \
141 return( PSA_ERROR_BAD_STATE );
142
Gilles Peskinee59236f2018-01-27 23:32:46 +0100143static psa_status_t mbedtls_to_psa_error( int ret )
144{
Gilles Peskinea5905292018-02-07 20:59:33 +0100145 /* If there's both a high-level code and low-level code, dispatch on
146 * the high-level code. */
147 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100148 {
149 case 0:
150 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100151
152 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
153 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
154 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
155 return( PSA_ERROR_NOT_SUPPORTED );
156 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
157 return( PSA_ERROR_HARDWARE_FAILURE );
158
159 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
160 return( PSA_ERROR_HARDWARE_FAILURE );
161
Gilles Peskine9a944802018-06-21 09:35:35 +0200162 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
163 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
164 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
165 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
166 case MBEDTLS_ERR_ASN1_INVALID_DATA:
167 return( PSA_ERROR_INVALID_ARGUMENT );
168 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
169 return( PSA_ERROR_INSUFFICIENT_MEMORY );
170 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
171 return( PSA_ERROR_BUFFER_TOO_SMALL );
172
Gilles Peskinea5905292018-02-07 20:59:33 +0100173 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
174 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
175 return( PSA_ERROR_NOT_SUPPORTED );
176 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
177 return( PSA_ERROR_HARDWARE_FAILURE );
178
179 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
180 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
181 return( PSA_ERROR_NOT_SUPPORTED );
182 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
183 return( PSA_ERROR_HARDWARE_FAILURE );
184
185 case MBEDTLS_ERR_CCM_BAD_INPUT:
186 return( PSA_ERROR_INVALID_ARGUMENT );
187 case MBEDTLS_ERR_CCM_AUTH_FAILED:
188 return( PSA_ERROR_INVALID_SIGNATURE );
189 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
190 return( PSA_ERROR_HARDWARE_FAILURE );
191
192 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
193 return( PSA_ERROR_NOT_SUPPORTED );
194 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
195 return( PSA_ERROR_INVALID_ARGUMENT );
196 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
197 return( PSA_ERROR_INSUFFICIENT_MEMORY );
198 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
199 return( PSA_ERROR_INVALID_PADDING );
200 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
201 return( PSA_ERROR_BAD_STATE );
202 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
203 return( PSA_ERROR_INVALID_SIGNATURE );
204 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
205 return( PSA_ERROR_TAMPERING_DETECTED );
206 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
207 return( PSA_ERROR_HARDWARE_FAILURE );
208
209 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
210 return( PSA_ERROR_HARDWARE_FAILURE );
211
212 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
213 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
214 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
215 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
216 return( PSA_ERROR_NOT_SUPPORTED );
217 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
218 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
219
220 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
221 return( PSA_ERROR_NOT_SUPPORTED );
222 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
223 return( PSA_ERROR_HARDWARE_FAILURE );
224
Gilles Peskinee59236f2018-01-27 23:32:46 +0100225 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
226 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
227 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
228 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100229
230 case MBEDTLS_ERR_GCM_AUTH_FAILED:
231 return( PSA_ERROR_INVALID_SIGNATURE );
232 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200233 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100234 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
235 return( PSA_ERROR_HARDWARE_FAILURE );
236
237 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
238 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
239 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
240 return( PSA_ERROR_HARDWARE_FAILURE );
241
242 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
243 return( PSA_ERROR_NOT_SUPPORTED );
244 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
245 return( PSA_ERROR_INVALID_ARGUMENT );
246 case MBEDTLS_ERR_MD_ALLOC_FAILED:
247 return( PSA_ERROR_INSUFFICIENT_MEMORY );
248 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
249 return( PSA_ERROR_STORAGE_FAILURE );
250 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
251 return( PSA_ERROR_HARDWARE_FAILURE );
252
Gilles Peskinef76aa772018-10-29 19:24:33 +0100253 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
254 return( PSA_ERROR_STORAGE_FAILURE );
255 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
256 return( PSA_ERROR_INVALID_ARGUMENT );
257 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
258 return( PSA_ERROR_INVALID_ARGUMENT );
259 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
260 return( PSA_ERROR_BUFFER_TOO_SMALL );
261 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
262 return( PSA_ERROR_INVALID_ARGUMENT );
263 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
264 return( PSA_ERROR_INVALID_ARGUMENT );
265 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
266 return( PSA_ERROR_INVALID_ARGUMENT );
267 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
268 return( PSA_ERROR_INSUFFICIENT_MEMORY );
269
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100270 case MBEDTLS_ERR_PK_ALLOC_FAILED:
271 return( PSA_ERROR_INSUFFICIENT_MEMORY );
272 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
273 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
274 return( PSA_ERROR_INVALID_ARGUMENT );
275 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100276 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100277 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
278 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
279 return( PSA_ERROR_INVALID_ARGUMENT );
280 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
281 return( PSA_ERROR_NOT_SUPPORTED );
282 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
283 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
284 return( PSA_ERROR_NOT_PERMITTED );
285 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
286 return( PSA_ERROR_INVALID_ARGUMENT );
287 case MBEDTLS_ERR_PK_INVALID_ALG:
288 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
289 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
290 return( PSA_ERROR_NOT_SUPPORTED );
291 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
292 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100293 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
294 return( PSA_ERROR_HARDWARE_FAILURE );
295
296 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
297 return( PSA_ERROR_HARDWARE_FAILURE );
298
299 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
300 return( PSA_ERROR_INVALID_ARGUMENT );
301 case MBEDTLS_ERR_RSA_INVALID_PADDING:
302 return( PSA_ERROR_INVALID_PADDING );
303 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
304 return( PSA_ERROR_HARDWARE_FAILURE );
305 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
306 return( PSA_ERROR_INVALID_ARGUMENT );
307 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
308 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
309 return( PSA_ERROR_TAMPERING_DETECTED );
310 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
311 return( PSA_ERROR_INVALID_SIGNATURE );
312 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
313 return( PSA_ERROR_BUFFER_TOO_SMALL );
314 case MBEDTLS_ERR_RSA_RNG_FAILED:
315 return( PSA_ERROR_INSUFFICIENT_MEMORY );
316 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
317 return( PSA_ERROR_NOT_SUPPORTED );
318 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
319 return( PSA_ERROR_HARDWARE_FAILURE );
320
321 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
322 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
323 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
324 return( PSA_ERROR_HARDWARE_FAILURE );
325
326 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
327 return( PSA_ERROR_INVALID_ARGUMENT );
328 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
329 return( PSA_ERROR_HARDWARE_FAILURE );
330
itayzafrir5c753392018-05-08 11:18:38 +0300331 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300332 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300333 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300334 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
335 return( PSA_ERROR_BUFFER_TOO_SMALL );
336 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
337 return( PSA_ERROR_NOT_SUPPORTED );
338 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
339 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
340 return( PSA_ERROR_INVALID_SIGNATURE );
341 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
342 return( PSA_ERROR_INSUFFICIENT_MEMORY );
343 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
344 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300345
Gilles Peskinee59236f2018-01-27 23:32:46 +0100346 default:
347 return( PSA_ERROR_UNKNOWN_ERROR );
348 }
349}
350
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200351
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200352
353
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100354/****************************************************************/
355/* Key management */
356/****************************************************************/
357
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100358#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200359static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
360{
361 switch( grpid )
362 {
363 case MBEDTLS_ECP_DP_SECP192R1:
364 return( PSA_ECC_CURVE_SECP192R1 );
365 case MBEDTLS_ECP_DP_SECP224R1:
366 return( PSA_ECC_CURVE_SECP224R1 );
367 case MBEDTLS_ECP_DP_SECP256R1:
368 return( PSA_ECC_CURVE_SECP256R1 );
369 case MBEDTLS_ECP_DP_SECP384R1:
370 return( PSA_ECC_CURVE_SECP384R1 );
371 case MBEDTLS_ECP_DP_SECP521R1:
372 return( PSA_ECC_CURVE_SECP521R1 );
373 case MBEDTLS_ECP_DP_BP256R1:
374 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
375 case MBEDTLS_ECP_DP_BP384R1:
376 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
377 case MBEDTLS_ECP_DP_BP512R1:
378 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
379 case MBEDTLS_ECP_DP_CURVE25519:
380 return( PSA_ECC_CURVE_CURVE25519 );
381 case MBEDTLS_ECP_DP_SECP192K1:
382 return( PSA_ECC_CURVE_SECP192K1 );
383 case MBEDTLS_ECP_DP_SECP224K1:
384 return( PSA_ECC_CURVE_SECP224K1 );
385 case MBEDTLS_ECP_DP_SECP256K1:
386 return( PSA_ECC_CURVE_SECP256K1 );
387 case MBEDTLS_ECP_DP_CURVE448:
388 return( PSA_ECC_CURVE_CURVE448 );
389 default:
390 return( 0 );
391 }
392}
393
Gilles Peskine12313cd2018-06-20 00:20:32 +0200394static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
395{
396 switch( curve )
397 {
398 case PSA_ECC_CURVE_SECP192R1:
399 return( MBEDTLS_ECP_DP_SECP192R1 );
400 case PSA_ECC_CURVE_SECP224R1:
401 return( MBEDTLS_ECP_DP_SECP224R1 );
402 case PSA_ECC_CURVE_SECP256R1:
403 return( MBEDTLS_ECP_DP_SECP256R1 );
404 case PSA_ECC_CURVE_SECP384R1:
405 return( MBEDTLS_ECP_DP_SECP384R1 );
406 case PSA_ECC_CURVE_SECP521R1:
407 return( MBEDTLS_ECP_DP_SECP521R1 );
408 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
409 return( MBEDTLS_ECP_DP_BP256R1 );
410 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
411 return( MBEDTLS_ECP_DP_BP384R1 );
412 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
413 return( MBEDTLS_ECP_DP_BP512R1 );
414 case PSA_ECC_CURVE_CURVE25519:
415 return( MBEDTLS_ECP_DP_CURVE25519 );
416 case PSA_ECC_CURVE_SECP192K1:
417 return( MBEDTLS_ECP_DP_SECP192K1 );
418 case PSA_ECC_CURVE_SECP224K1:
419 return( MBEDTLS_ECP_DP_SECP224K1 );
420 case PSA_ECC_CURVE_SECP256K1:
421 return( MBEDTLS_ECP_DP_SECP256K1 );
422 case PSA_ECC_CURVE_CURVE448:
423 return( MBEDTLS_ECP_DP_CURVE448 );
424 default:
425 return( MBEDTLS_ECP_DP_NONE );
426 }
427}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100428#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200429
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200430static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
431 size_t bits,
432 struct raw_data *raw )
433{
434 /* Check that the bit size is acceptable for the key type */
435 switch( type )
436 {
437 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200438 if( bits == 0 )
439 {
440 raw->bytes = 0;
441 raw->data = NULL;
442 return( PSA_SUCCESS );
443 }
444 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200445#if defined(MBEDTLS_MD_C)
446 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200447#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200448 case PSA_KEY_TYPE_DERIVE:
449 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200450#if defined(MBEDTLS_AES_C)
451 case PSA_KEY_TYPE_AES:
452 if( bits != 128 && bits != 192 && bits != 256 )
453 return( PSA_ERROR_INVALID_ARGUMENT );
454 break;
455#endif
456#if defined(MBEDTLS_CAMELLIA_C)
457 case PSA_KEY_TYPE_CAMELLIA:
458 if( bits != 128 && bits != 192 && bits != 256 )
459 return( PSA_ERROR_INVALID_ARGUMENT );
460 break;
461#endif
462#if defined(MBEDTLS_DES_C)
463 case PSA_KEY_TYPE_DES:
464 if( bits != 64 && bits != 128 && bits != 192 )
465 return( PSA_ERROR_INVALID_ARGUMENT );
466 break;
467#endif
468#if defined(MBEDTLS_ARC4_C)
469 case PSA_KEY_TYPE_ARC4:
470 if( bits < 8 || bits > 2048 )
471 return( PSA_ERROR_INVALID_ARGUMENT );
472 break;
473#endif
474 default:
475 return( PSA_ERROR_NOT_SUPPORTED );
476 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200477 if( bits % 8 != 0 )
478 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200479
480 /* Allocate memory for the key */
481 raw->bytes = PSA_BITS_TO_BYTES( bits );
482 raw->data = mbedtls_calloc( 1, raw->bytes );
483 if( raw->data == NULL )
484 {
485 raw->bytes = 0;
486 return( PSA_ERROR_INSUFFICIENT_MEMORY );
487 }
488 return( PSA_SUCCESS );
489}
490
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200491#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100492/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
493 * that are not a multiple of 8) well. For example, there is only
494 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
495 * way to return the exact bit size of a key.
496 * To keep things simple, reject non-byte-aligned key sizes. */
497static psa_status_t psa_check_rsa_key_byte_aligned(
498 const mbedtls_rsa_context *rsa )
499{
500 mbedtls_mpi n;
501 psa_status_t status;
502 mbedtls_mpi_init( &n );
503 status = mbedtls_to_psa_error(
504 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
505 if( status == PSA_SUCCESS )
506 {
507 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
508 status = PSA_ERROR_NOT_SUPPORTED;
509 }
510 mbedtls_mpi_free( &n );
511 return( status );
512}
513
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200514static psa_status_t psa_import_rsa_key( mbedtls_pk_context *pk,
515 mbedtls_rsa_context **p_rsa )
516{
517 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_RSA )
518 return( PSA_ERROR_INVALID_ARGUMENT );
519 else
520 {
521 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *pk );
Gilles Peskineaac64a22018-11-12 18:37:42 +0100522 /* The size of an RSA key doesn't have to be a multiple of 8.
523 * Mbed TLS supports non-byte-aligned key sizes, but not well.
524 * For example, mbedtls_rsa_get_len() returns the key size in
525 * bytes, not in bits. */
526 size_t bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
Gilles Peskine86a440b2018-11-12 18:39:40 +0100527 psa_status_t status;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200528 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
529 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +0100530 status = psa_check_rsa_key_byte_aligned( rsa );
531 if( status != PSA_SUCCESS )
532 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200533 *p_rsa = rsa;
534 return( PSA_SUCCESS );
535 }
536}
537#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
538
539#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinef76aa772018-10-29 19:24:33 +0100540/* Import an elliptic curve parsed by the mbedtls pk module. */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200541static psa_status_t psa_import_ecp_key( psa_ecc_curve_t expected_curve,
542 mbedtls_pk_context *pk,
543 mbedtls_ecp_keypair **p_ecp )
544{
545 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_ECKEY )
546 return( PSA_ERROR_INVALID_ARGUMENT );
547 else
548 {
549 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( *pk );
550 psa_ecc_curve_t actual_curve = mbedtls_ecc_group_to_psa( ecp->grp.id );
551 if( actual_curve != expected_curve )
552 return( PSA_ERROR_INVALID_ARGUMENT );
553 *p_ecp = ecp;
554 return( PSA_SUCCESS );
555 }
556}
557#endif /* defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C) */
558
Gilles Peskinef76aa772018-10-29 19:24:33 +0100559#if defined(MBEDTLS_ECP_C)
560/* Import a private key given as a byte string which is the private value
561 * in big-endian order. */
562static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
563 const uint8_t *data,
564 size_t data_length,
565 mbedtls_ecp_keypair **p_ecp )
566{
567 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
568 mbedtls_ecp_keypair *ecp = NULL;
569 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
570
571 *p_ecp = NULL;
572 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
573 if( ecp == NULL )
574 return( PSA_ERROR_INSUFFICIENT_MEMORY );
575
576 /* Load the group. */
577 status = mbedtls_to_psa_error(
578 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
579 if( status != PSA_SUCCESS )
580 goto exit;
581 /* Load the secret value. */
582 status = mbedtls_to_psa_error(
583 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
584 if( status != PSA_SUCCESS )
585 goto exit;
586 /* Validate the private key. */
587 status = mbedtls_to_psa_error(
588 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
589 if( status != PSA_SUCCESS )
590 goto exit;
591 /* Calculate the public key from the private key. */
592 status = mbedtls_to_psa_error(
593 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
594 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
595 if( status != PSA_SUCCESS )
596 goto exit;
597
598 *p_ecp = ecp;
599 return( PSA_SUCCESS );
600
601exit:
602 if( ecp != NULL )
603 {
604 mbedtls_ecp_keypair_free( ecp );
605 mbedtls_free( ecp );
606 }
607 return( status );
608}
609#endif /* defined(MBEDTLS_ECP_C) */
610
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100611/** Import key data into a slot. `slot->type` must have been set
612 * previously. This function assumes that the slot does not contain
613 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100614static psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
Darryl Green940d72c2018-07-13 13:18:51 +0100615 const uint8_t *data,
616 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100617{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200618 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100619
Darryl Green940d72c2018-07-13 13:18:51 +0100620 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100621 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100622 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100623 if( data_length > SIZE_MAX / 8 )
624 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green940d72c2018-07-13 13:18:51 +0100625 status = prepare_raw_data_slot( slot->type,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200626 PSA_BYTES_TO_BITS( data_length ),
627 &slot->data.raw );
628 if( status != PSA_SUCCESS )
629 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200630 if( data_length != 0 )
631 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100632 }
633 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100634#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100635 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100636 {
Darryl Green940d72c2018-07-13 13:18:51 +0100637 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100638 data, data_length,
639 &slot->data.ecp );
640 if( status != PSA_SUCCESS )
641 return( status );
642 }
643 else
644#endif /* MBEDTLS_ECP_C */
Gilles Peskine969ac722018-01-28 18:16:59 +0100645#if defined(MBEDTLS_PK_PARSE_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100646 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
647 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100648 {
649 int ret;
Gilles Peskine969ac722018-01-28 18:16:59 +0100650 mbedtls_pk_context pk;
651 mbedtls_pk_init( &pk );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200652
653 /* Parse the data. */
Darryl Green940d72c2018-07-13 13:18:51 +0100654 if( PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100655 ret = mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 );
656 else
657 ret = mbedtls_pk_parse_public_key( &pk, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100658 if( ret != 0 )
659 return( mbedtls_to_psa_error( ret ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200660
661 /* We have something that the pkparse module recognizes.
662 * If it has the expected type and passes any type-specific
663 * checks, store it. */
Gilles Peskine969ac722018-01-28 18:16:59 +0100664#if defined(MBEDTLS_RSA_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100665 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200666 status = psa_import_rsa_key( &pk, &slot->data.rsa );
667 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100668#endif /* MBEDTLS_RSA_C */
669#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100670 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
671 status = psa_import_ecp_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200672 &pk, &slot->data.ecp );
673 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100674#endif /* MBEDTLS_ECP_C */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200675 {
676 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinec648d692018-06-28 08:46:13 +0200677 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200678
Gilles Peskinec648d692018-06-28 08:46:13 +0200679 /* Free the content of the pk object only on error. On success,
680 * the content of the object has been stored in the slot. */
681 if( status != PSA_SUCCESS )
682 {
683 mbedtls_pk_free( &pk );
684 return( status );
Gilles Peskine969ac722018-01-28 18:16:59 +0100685 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100686 }
687 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100688#endif /* defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100689 {
690 return( PSA_ERROR_NOT_SUPPORTED );
691 }
Darryl Green940d72c2018-07-13 13:18:51 +0100692 return( PSA_SUCCESS );
693}
694
Darryl Greend49a4992018-06-18 17:27:26 +0100695#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine2f060a82018-12-04 17:12:32 +0100696static psa_status_t psa_load_persistent_key_into_slot( psa_key_slot_t *p_slot )
Darryl Greend49a4992018-06-18 17:27:26 +0100697{
698 psa_status_t status = PSA_SUCCESS;
699 uint8_t *key_data = NULL;
700 size_t key_data_length = 0;
701
Gilles Peskine69f976b2018-11-30 18:46:56 +0100702 status = psa_load_persistent_key( p_slot->persistent_storage_id,
703 &( p_slot )->type,
Darryl Greend49a4992018-06-18 17:27:26 +0100704 &( p_slot )->policy, &key_data,
705 &key_data_length );
706 if( status != PSA_SUCCESS )
707 goto exit;
708 status = psa_import_key_into_slot( p_slot,
709 key_data, key_data_length );
710exit:
711 psa_free_persistent_key_data( key_data, key_data_length );
712 return( status );
713}
714#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
715
Darryl Green06fd18d2018-07-16 11:21:11 +0100716/* Retrieve an empty key slot (slot with no key data, but possibly
717 * with some metadata such as a policy). */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100718static psa_status_t psa_get_empty_key_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100719 psa_key_slot_t **p_slot )
Darryl Green06fd18d2018-07-16 11:21:11 +0100720{
721 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100722 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100723
724 *p_slot = NULL;
725
Gilles Peskinec5487a82018-12-03 18:08:14 +0100726 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100727 if( status != PSA_SUCCESS )
728 return( status );
729
730 if( slot->type != PSA_KEY_TYPE_NONE )
731 return( PSA_ERROR_OCCUPIED_SLOT );
732
733 *p_slot = slot;
734 return( status );
735}
736
737/** Retrieve a slot which must contain a key. The key must have allow all the
738 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
739 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100740static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100741 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100742 psa_key_usage_t usage,
743 psa_algorithm_t alg )
744{
745 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100746 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100747
748 *p_slot = NULL;
749
Gilles Peskinec5487a82018-12-03 18:08:14 +0100750 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100751 if( status != PSA_SUCCESS )
752 return( status );
753 if( slot->type == PSA_KEY_TYPE_NONE )
754 return( PSA_ERROR_EMPTY_SLOT );
755
756 /* Enforce that usage policy for the key slot contains all the flags
757 * required by the usage parameter. There is one exception: public
758 * keys can always be exported, so we treat public key objects as
759 * if they had the export flag. */
760 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
761 usage &= ~PSA_KEY_USAGE_EXPORT;
762 if( ( slot->policy.usage & usage ) != usage )
763 return( PSA_ERROR_NOT_PERMITTED );
764 if( alg != 0 && ( alg != slot->policy.alg ) )
765 return( PSA_ERROR_NOT_PERMITTED );
766
767 *p_slot = slot;
768 return( PSA_SUCCESS );
769}
Darryl Green940d72c2018-07-13 13:18:51 +0100770
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100771/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100772static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000773{
774 if( slot->type == PSA_KEY_TYPE_NONE )
775 {
776 /* No key material to clean. */
777 }
778 else if( key_type_is_raw_bytes( slot->type ) )
779 {
780 mbedtls_free( slot->data.raw.data );
781 }
782 else
783#if defined(MBEDTLS_RSA_C)
784 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
785 {
786 mbedtls_rsa_free( slot->data.rsa );
787 mbedtls_free( slot->data.rsa );
788 }
789 else
790#endif /* defined(MBEDTLS_RSA_C) */
791#if defined(MBEDTLS_ECP_C)
792 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
793 {
794 mbedtls_ecp_keypair_free( slot->data.ecp );
795 mbedtls_free( slot->data.ecp );
796 }
797 else
798#endif /* defined(MBEDTLS_ECP_C) */
799 {
800 /* Shouldn't happen: the key type is not any type that we
801 * put in. */
802 return( PSA_ERROR_TAMPERING_DETECTED );
803 }
804
805 return( PSA_SUCCESS );
806}
807
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100808/** Completely wipe a slot in memory, including its policy.
809 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100810psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100811{
812 psa_status_t status = psa_remove_key_data_from_memory( slot );
813 /* At this point, key material and other type-specific content has
814 * been wiped. Clear remaining metadata. We can call memset and not
815 * zeroize because the metadata is not particularly sensitive. */
816 memset( slot, 0, sizeof( *slot ) );
817 return( status );
818}
819
Gilles Peskine961849f2018-11-30 18:54:54 +0100820psa_status_t psa_internal_make_key_persistent( psa_key_handle_t handle,
821 psa_key_id_t id )
822{
Gilles Peskine4a044732018-12-03 18:19:39 +0100823#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine2f060a82018-12-04 17:12:32 +0100824 psa_key_slot_t *slot;
Gilles Peskine961849f2018-11-30 18:54:54 +0100825 psa_status_t status;
826
827 /* Reject id=0 because by general library conventions, 0 is an invalid
828 * value wherever possible. */
829 if( id == 0 )
830 return( PSA_ERROR_INVALID_ARGUMENT );
831 /* Reject high values because the file names are reserved for the
832 * library's internal use. */
Gilles Peskine48868122018-12-10 17:30:29 +0100833 if( id >= PSA_MAX_PERSISTENT_KEY_IDENTIFIER )
Gilles Peskine961849f2018-11-30 18:54:54 +0100834 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine961849f2018-11-30 18:54:54 +0100835
836 status = psa_get_key_slot( handle, &slot );
837 if( status != PSA_SUCCESS )
838 return( status );
839
840 slot->lifetime = PSA_KEY_LIFETIME_PERSISTENT;
841 slot->persistent_storage_id = id;
842 status = psa_load_persistent_key_into_slot( slot );
843
844 return( status );
Gilles Peskine4a044732018-12-03 18:19:39 +0100845
846#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
847 (void) handle;
848 (void) id;
849 return( PSA_ERROR_NOT_SUPPORTED );
850#endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine961849f2018-11-30 18:54:54 +0100851}
852
853psa_status_t psa_internal_release_key_slot( psa_key_handle_t handle )
854{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100855 psa_key_slot_t *slot;
Gilles Peskinec5487a82018-12-03 18:08:14 +0100856 psa_status_t status;
857
858 status = psa_get_key_slot( handle, &slot );
859 if( status != PSA_SUCCESS )
860 return( status );
Gilles Peskinec5487a82018-12-03 18:08:14 +0100861
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100862 return( psa_wipe_key_slot( slot ) );
Gilles Peskine961849f2018-11-30 18:54:54 +0100863}
864
Gilles Peskinec5487a82018-12-03 18:08:14 +0100865psa_status_t psa_import_key( psa_key_handle_t handle,
Darryl Green940d72c2018-07-13 13:18:51 +0100866 psa_key_type_t type,
867 const uint8_t *data,
868 size_t data_length )
869{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100870 psa_key_slot_t *slot;
Darryl Green940d72c2018-07-13 13:18:51 +0100871 psa_status_t status;
872
Gilles Peskinec5487a82018-12-03 18:08:14 +0100873 status = psa_get_empty_key_slot( handle, &slot );
Darryl Green940d72c2018-07-13 13:18:51 +0100874 if( status != PSA_SUCCESS )
875 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100876
877 slot->type = type;
Darryl Green940d72c2018-07-13 13:18:51 +0100878
879 status = psa_import_key_into_slot( slot, data, data_length );
880 if( status != PSA_SUCCESS )
881 {
882 slot->type = PSA_KEY_TYPE_NONE;
883 return( status );
884 }
885
Darryl Greend49a4992018-06-18 17:27:26 +0100886#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
887 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
888 {
889 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +0100890 status = psa_save_persistent_key( slot->persistent_storage_id,
891 slot->type, &slot->policy, data,
Darryl Greend49a4992018-06-18 17:27:26 +0100892 data_length );
893 if( status != PSA_SUCCESS )
894 {
895 (void) psa_remove_key_data_from_memory( slot );
896 slot->type = PSA_KEY_TYPE_NONE;
897 }
898 }
899#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
900
901 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100902}
903
Gilles Peskinec5487a82018-12-03 18:08:14 +0100904psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100905{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100906 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100907 psa_status_t status = PSA_SUCCESS;
908 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100909
Gilles Peskinec5487a82018-12-03 18:08:14 +0100910 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200911 if( status != PSA_SUCCESS )
912 return( status );
Darryl Greend49a4992018-06-18 17:27:26 +0100913#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
914 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
915 {
Gilles Peskine69f976b2018-11-30 18:46:56 +0100916 storage_status =
917 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +0100918 }
919#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100920 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +0100921 if( status != PSA_SUCCESS )
922 return( status );
923 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100924}
925
Gilles Peskineb870b182018-07-06 16:02:09 +0200926/* Return the size of the key in the given slot, in bits. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100927static size_t psa_get_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +0200928{
929 if( key_type_is_raw_bytes( slot->type ) )
930 return( slot->data.raw.bytes * 8 );
931#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200932 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +0100933 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +0200934#endif /* defined(MBEDTLS_RSA_C) */
935#if defined(MBEDTLS_ECP_C)
936 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
937 return( slot->data.ecp->grp.pbits );
938#endif /* defined(MBEDTLS_ECP_C) */
939 /* Shouldn't happen except on an empty slot. */
940 return( 0 );
941}
942
Gilles Peskinec5487a82018-12-03 18:08:14 +0100943psa_status_t psa_get_key_information( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +0200944 psa_key_type_t *type,
945 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100946{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100947 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200948 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100949
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100950 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200951 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100952 if( bits != NULL )
953 *bits = 0;
Gilles Peskinec5487a82018-12-03 18:08:14 +0100954 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200955 if( status != PSA_SUCCESS )
956 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +0200957
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100958 if( slot->type == PSA_KEY_TYPE_NONE )
959 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200960 if( type != NULL )
961 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +0200962 if( bits != NULL )
963 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100964 return( PSA_SUCCESS );
965}
966
Gilles Peskine2f060a82018-12-04 17:12:32 +0100967static psa_status_t psa_internal_export_key( psa_key_slot_t *slot,
Gilles Peskine2d277862018-06-18 15:41:12 +0200968 uint8_t *data,
969 size_t data_size,
970 size_t *data_length,
971 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100972{
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100973 *data_length = 0;
974
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200975 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300976 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +0300977
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200978 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100979 {
980 if( slot->data.raw.bytes > data_size )
981 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +0100982 if( data_size != 0 )
983 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200984 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +0100985 memset( data + slot->data.raw.bytes, 0,
986 data_size - slot->data.raw.bytes );
987 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100988 *data_length = slot->data.raw.bytes;
989 return( PSA_SUCCESS );
990 }
Gilles Peskine188c71e2018-10-29 19:26:02 +0100991#if defined(MBEDTLS_ECP_C)
992 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
993 {
Darryl Greendd8fb772018-11-07 16:00:44 +0000994 psa_status_t status;
995
Gilles Peskine188c71e2018-10-29 19:26:02 +0100996 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( slot ) );
997 if( bytes > data_size )
998 return( PSA_ERROR_BUFFER_TOO_SMALL );
999 status = mbedtls_to_psa_error(
1000 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1001 if( status != PSA_SUCCESS )
1002 return( status );
1003 memset( data + bytes, 0, data_size - bytes );
1004 *data_length = bytes;
1005 return( PSA_SUCCESS );
1006 }
1007#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001008 else
Moran Peker17e36e12018-05-02 12:55:20 +03001009 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001010#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001011 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001012 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001013 {
Moran Pekera998bc62018-04-16 18:16:20 +03001014 mbedtls_pk_context pk;
1015 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001016 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001017 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001018#if defined(MBEDTLS_RSA_C)
1019 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001020 pk.pk_info = &mbedtls_rsa_info;
1021 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001022#else
1023 return( PSA_ERROR_NOT_SUPPORTED );
1024#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001025 }
1026 else
1027 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001028#if defined(MBEDTLS_ECP_C)
1029 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001030 pk.pk_info = &mbedtls_eckey_info;
1031 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001032#else
1033 return( PSA_ERROR_NOT_SUPPORTED );
1034#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001035 }
Moran Pekerd7326592018-05-29 16:56:39 +03001036 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001037 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +03001038 else
1039 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +03001040 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001041 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001042 /* If data_size is 0 then data may be NULL and then the
1043 * call to memset would have undefined behavior. */
1044 if( data_size != 0 )
1045 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001046 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001047 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001048 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1049 * Move the data to the beginning and erase remaining data
1050 * at the original location. */
1051 if( 2 * (size_t) ret <= data_size )
1052 {
1053 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001054 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001055 }
1056 else if( (size_t) ret < data_size )
1057 {
1058 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001059 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001060 }
Moran Pekera998bc62018-04-16 18:16:20 +03001061 *data_length = ret;
1062 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001063 }
1064 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001065#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001066 {
1067 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001068 it is valid for a special-purpose implementation to omit
1069 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001070 return( PSA_ERROR_NOT_SUPPORTED );
1071 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001072 }
1073}
1074
Gilles Peskinec5487a82018-12-03 18:08:14 +01001075psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001076 uint8_t *data,
1077 size_t data_size,
1078 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001079{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001080 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001081 psa_status_t status;
1082
1083 /* Set the key to empty now, so that even when there are errors, we always
1084 * set data_length to a value between 0 and data_size. On error, setting
1085 * the key to empty is a good choice because an empty key representation is
1086 * unlikely to be accepted anywhere. */
1087 *data_length = 0;
1088
1089 /* Export requires the EXPORT flag. There is an exception for public keys,
1090 * which don't require any flag, but psa_get_key_from_slot takes
1091 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001092 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001093 if( status != PSA_SUCCESS )
1094 return( status );
1095 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001096 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001097}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001098
Gilles Peskinec5487a82018-12-03 18:08:14 +01001099psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001100 uint8_t *data,
1101 size_t data_size,
1102 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001103{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001104 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001105 psa_status_t status;
1106
1107 /* Set the key to empty now, so that even when there are errors, we always
1108 * set data_length to a value between 0 and data_size. On error, setting
1109 * the key to empty is a good choice because an empty key representation is
1110 * unlikely to be accepted anywhere. */
1111 *data_length = 0;
1112
1113 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001114 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001115 if( status != PSA_SUCCESS )
1116 return( status );
1117 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001118 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001119}
1120
Darryl Green0c6575a2018-11-07 16:05:30 +00001121#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine2f060a82018-12-04 17:12:32 +01001122static psa_status_t psa_save_generated_persistent_key( psa_key_slot_t *slot,
Darryl Green0c6575a2018-11-07 16:05:30 +00001123 size_t bits )
1124{
1125 psa_status_t status;
1126 uint8_t *data;
1127 size_t key_length;
1128 size_t data_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type, bits );
1129 data = mbedtls_calloc( 1, data_size );
itayzafrir910c76b2018-11-21 16:03:21 +02001130 if( data == NULL )
1131 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Darryl Green0c6575a2018-11-07 16:05:30 +00001132 /* Get key data in export format */
1133 status = psa_internal_export_key( slot, data, data_size, &key_length, 0 );
1134 if( status != PSA_SUCCESS )
1135 {
1136 slot->type = PSA_KEY_TYPE_NONE;
1137 goto exit;
1138 }
1139 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +01001140 status = psa_save_persistent_key( slot->persistent_storage_id,
1141 slot->type, &slot->policy,
Darryl Green0c6575a2018-11-07 16:05:30 +00001142 data, key_length );
1143 if( status != PSA_SUCCESS )
1144 {
1145 slot->type = PSA_KEY_TYPE_NONE;
1146 }
1147exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01001148 mbedtls_platform_zeroize( data, key_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00001149 mbedtls_free( data );
1150 return( status );
1151}
1152#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1153
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001154
1155
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001156/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001157/* Message digests */
1158/****************************************************************/
1159
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001160static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001161{
1162 switch( alg )
1163 {
1164#if defined(MBEDTLS_MD2_C)
1165 case PSA_ALG_MD2:
1166 return( &mbedtls_md2_info );
1167#endif
1168#if defined(MBEDTLS_MD4_C)
1169 case PSA_ALG_MD4:
1170 return( &mbedtls_md4_info );
1171#endif
1172#if defined(MBEDTLS_MD5_C)
1173 case PSA_ALG_MD5:
1174 return( &mbedtls_md5_info );
1175#endif
1176#if defined(MBEDTLS_RIPEMD160_C)
1177 case PSA_ALG_RIPEMD160:
1178 return( &mbedtls_ripemd160_info );
1179#endif
1180#if defined(MBEDTLS_SHA1_C)
1181 case PSA_ALG_SHA_1:
1182 return( &mbedtls_sha1_info );
1183#endif
1184#if defined(MBEDTLS_SHA256_C)
1185 case PSA_ALG_SHA_224:
1186 return( &mbedtls_sha224_info );
1187 case PSA_ALG_SHA_256:
1188 return( &mbedtls_sha256_info );
1189#endif
1190#if defined(MBEDTLS_SHA512_C)
1191 case PSA_ALG_SHA_384:
1192 return( &mbedtls_sha384_info );
1193 case PSA_ALG_SHA_512:
1194 return( &mbedtls_sha512_info );
1195#endif
1196 default:
1197 return( NULL );
1198 }
1199}
1200
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001201psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1202{
1203 switch( operation->alg )
1204 {
Gilles Peskine81736312018-06-26 15:04:31 +02001205 case 0:
1206 /* The object has (apparently) been initialized but it is not
1207 * in use. It's ok to call abort on such an object, and there's
1208 * nothing to do. */
1209 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001210#if defined(MBEDTLS_MD2_C)
1211 case PSA_ALG_MD2:
1212 mbedtls_md2_free( &operation->ctx.md2 );
1213 break;
1214#endif
1215#if defined(MBEDTLS_MD4_C)
1216 case PSA_ALG_MD4:
1217 mbedtls_md4_free( &operation->ctx.md4 );
1218 break;
1219#endif
1220#if defined(MBEDTLS_MD5_C)
1221 case PSA_ALG_MD5:
1222 mbedtls_md5_free( &operation->ctx.md5 );
1223 break;
1224#endif
1225#if defined(MBEDTLS_RIPEMD160_C)
1226 case PSA_ALG_RIPEMD160:
1227 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1228 break;
1229#endif
1230#if defined(MBEDTLS_SHA1_C)
1231 case PSA_ALG_SHA_1:
1232 mbedtls_sha1_free( &operation->ctx.sha1 );
1233 break;
1234#endif
1235#if defined(MBEDTLS_SHA256_C)
1236 case PSA_ALG_SHA_224:
1237 case PSA_ALG_SHA_256:
1238 mbedtls_sha256_free( &operation->ctx.sha256 );
1239 break;
1240#endif
1241#if defined(MBEDTLS_SHA512_C)
1242 case PSA_ALG_SHA_384:
1243 case PSA_ALG_SHA_512:
1244 mbedtls_sha512_free( &operation->ctx.sha512 );
1245 break;
1246#endif
1247 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001248 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001249 }
1250 operation->alg = 0;
1251 return( PSA_SUCCESS );
1252}
1253
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001254psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001255 psa_algorithm_t alg )
1256{
1257 int ret;
1258 operation->alg = 0;
1259 switch( alg )
1260 {
1261#if defined(MBEDTLS_MD2_C)
1262 case PSA_ALG_MD2:
1263 mbedtls_md2_init( &operation->ctx.md2 );
1264 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1265 break;
1266#endif
1267#if defined(MBEDTLS_MD4_C)
1268 case PSA_ALG_MD4:
1269 mbedtls_md4_init( &operation->ctx.md4 );
1270 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1271 break;
1272#endif
1273#if defined(MBEDTLS_MD5_C)
1274 case PSA_ALG_MD5:
1275 mbedtls_md5_init( &operation->ctx.md5 );
1276 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1277 break;
1278#endif
1279#if defined(MBEDTLS_RIPEMD160_C)
1280 case PSA_ALG_RIPEMD160:
1281 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1282 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1283 break;
1284#endif
1285#if defined(MBEDTLS_SHA1_C)
1286 case PSA_ALG_SHA_1:
1287 mbedtls_sha1_init( &operation->ctx.sha1 );
1288 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1289 break;
1290#endif
1291#if defined(MBEDTLS_SHA256_C)
1292 case PSA_ALG_SHA_224:
1293 mbedtls_sha256_init( &operation->ctx.sha256 );
1294 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1295 break;
1296 case PSA_ALG_SHA_256:
1297 mbedtls_sha256_init( &operation->ctx.sha256 );
1298 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1299 break;
1300#endif
1301#if defined(MBEDTLS_SHA512_C)
1302 case PSA_ALG_SHA_384:
1303 mbedtls_sha512_init( &operation->ctx.sha512 );
1304 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1305 break;
1306 case PSA_ALG_SHA_512:
1307 mbedtls_sha512_init( &operation->ctx.sha512 );
1308 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1309 break;
1310#endif
1311 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001312 return( PSA_ALG_IS_HASH( alg ) ?
1313 PSA_ERROR_NOT_SUPPORTED :
1314 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001315 }
1316 if( ret == 0 )
1317 operation->alg = alg;
1318 else
1319 psa_hash_abort( operation );
1320 return( mbedtls_to_psa_error( ret ) );
1321}
1322
1323psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1324 const uint8_t *input,
1325 size_t input_length )
1326{
1327 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001328
1329 /* Don't require hash implementations to behave correctly on a
1330 * zero-length input, which may have an invalid pointer. */
1331 if( input_length == 0 )
1332 return( PSA_SUCCESS );
1333
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001334 switch( operation->alg )
1335 {
1336#if defined(MBEDTLS_MD2_C)
1337 case PSA_ALG_MD2:
1338 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1339 input, input_length );
1340 break;
1341#endif
1342#if defined(MBEDTLS_MD4_C)
1343 case PSA_ALG_MD4:
1344 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1345 input, input_length );
1346 break;
1347#endif
1348#if defined(MBEDTLS_MD5_C)
1349 case PSA_ALG_MD5:
1350 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1351 input, input_length );
1352 break;
1353#endif
1354#if defined(MBEDTLS_RIPEMD160_C)
1355 case PSA_ALG_RIPEMD160:
1356 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1357 input, input_length );
1358 break;
1359#endif
1360#if defined(MBEDTLS_SHA1_C)
1361 case PSA_ALG_SHA_1:
1362 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1363 input, input_length );
1364 break;
1365#endif
1366#if defined(MBEDTLS_SHA256_C)
1367 case PSA_ALG_SHA_224:
1368 case PSA_ALG_SHA_256:
1369 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1370 input, input_length );
1371 break;
1372#endif
1373#if defined(MBEDTLS_SHA512_C)
1374 case PSA_ALG_SHA_384:
1375 case PSA_ALG_SHA_512:
1376 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1377 input, input_length );
1378 break;
1379#endif
1380 default:
1381 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1382 break;
1383 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001384
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001385 if( ret != 0 )
1386 psa_hash_abort( operation );
1387 return( mbedtls_to_psa_error( ret ) );
1388}
1389
1390psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1391 uint8_t *hash,
1392 size_t hash_size,
1393 size_t *hash_length )
1394{
itayzafrir40835d42018-08-02 13:14:17 +03001395 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001396 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001397 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001398
1399 /* Fill the output buffer with something that isn't a valid hash
1400 * (barring an attack on the hash and deliberately-crafted input),
1401 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001402 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001403 /* If hash_size is 0 then hash may be NULL and then the
1404 * call to memset would have undefined behavior. */
1405 if( hash_size != 0 )
1406 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001407
1408 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001409 {
1410 status = PSA_ERROR_BUFFER_TOO_SMALL;
1411 goto exit;
1412 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001413
1414 switch( operation->alg )
1415 {
1416#if defined(MBEDTLS_MD2_C)
1417 case PSA_ALG_MD2:
1418 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1419 break;
1420#endif
1421#if defined(MBEDTLS_MD4_C)
1422 case PSA_ALG_MD4:
1423 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1424 break;
1425#endif
1426#if defined(MBEDTLS_MD5_C)
1427 case PSA_ALG_MD5:
1428 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1429 break;
1430#endif
1431#if defined(MBEDTLS_RIPEMD160_C)
1432 case PSA_ALG_RIPEMD160:
1433 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1434 break;
1435#endif
1436#if defined(MBEDTLS_SHA1_C)
1437 case PSA_ALG_SHA_1:
1438 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1439 break;
1440#endif
1441#if defined(MBEDTLS_SHA256_C)
1442 case PSA_ALG_SHA_224:
1443 case PSA_ALG_SHA_256:
1444 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1445 break;
1446#endif
1447#if defined(MBEDTLS_SHA512_C)
1448 case PSA_ALG_SHA_384:
1449 case PSA_ALG_SHA_512:
1450 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1451 break;
1452#endif
1453 default:
1454 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1455 break;
1456 }
itayzafrir40835d42018-08-02 13:14:17 +03001457 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001458
itayzafrir40835d42018-08-02 13:14:17 +03001459exit:
1460 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001461 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001462 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001463 return( psa_hash_abort( operation ) );
1464 }
1465 else
1466 {
1467 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001468 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001469 }
1470}
1471
Gilles Peskine2d277862018-06-18 15:41:12 +02001472psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1473 const uint8_t *hash,
1474 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001475{
1476 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1477 size_t actual_hash_length;
1478 psa_status_t status = psa_hash_finish( operation,
1479 actual_hash, sizeof( actual_hash ),
1480 &actual_hash_length );
1481 if( status != PSA_SUCCESS )
1482 return( status );
1483 if( actual_hash_length != hash_length )
1484 return( PSA_ERROR_INVALID_SIGNATURE );
1485 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1486 return( PSA_ERROR_INVALID_SIGNATURE );
1487 return( PSA_SUCCESS );
1488}
1489
1490
1491
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001492/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001493/* MAC */
1494/****************************************************************/
1495
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001496static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001497 psa_algorithm_t alg,
1498 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001499 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001500 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001501{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001502 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001503 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001504
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001505 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001506 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001507
Gilles Peskine8c9def32018-02-08 10:02:12 +01001508 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1509 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001510 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001511 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001512 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001513 mode = MBEDTLS_MODE_STREAM;
1514 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001515 case PSA_ALG_CTR:
1516 mode = MBEDTLS_MODE_CTR;
1517 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001518 case PSA_ALG_CFB:
1519 mode = MBEDTLS_MODE_CFB;
1520 break;
1521 case PSA_ALG_OFB:
1522 mode = MBEDTLS_MODE_OFB;
1523 break;
1524 case PSA_ALG_CBC_NO_PADDING:
1525 mode = MBEDTLS_MODE_CBC;
1526 break;
1527 case PSA_ALG_CBC_PKCS7:
1528 mode = MBEDTLS_MODE_CBC;
1529 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001530 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001531 mode = MBEDTLS_MODE_CCM;
1532 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001533 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001534 mode = MBEDTLS_MODE_GCM;
1535 break;
1536 default:
1537 return( NULL );
1538 }
1539 }
1540 else if( alg == PSA_ALG_CMAC )
1541 mode = MBEDTLS_MODE_ECB;
1542 else if( alg == PSA_ALG_GMAC )
1543 mode = MBEDTLS_MODE_GCM;
1544 else
1545 return( NULL );
1546
1547 switch( key_type )
1548 {
1549 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001550 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001551 break;
1552 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001553 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1554 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001555 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001556 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001557 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001558 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001559 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1560 * but two-key Triple-DES is functionally three-key Triple-DES
1561 * with K1=K3, so that's how we present it to mbedtls. */
1562 if( key_bits == 128 )
1563 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001564 break;
1565 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001566 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001567 break;
1568 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001569 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001570 break;
1571 default:
1572 return( NULL );
1573 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001574 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001575 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001576
Jaeden Amero23bbb752018-06-26 14:16:54 +01001577 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1578 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001579}
1580
Gilles Peskinea05219c2018-11-16 16:02:56 +01001581#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001582static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001583{
Gilles Peskine2d277862018-06-18 15:41:12 +02001584 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001585 {
1586 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001587 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001588 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001589 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001590 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001591 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001592 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001593 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001594 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001595 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001596 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001597 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001598 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001599 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001600 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001601 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001602 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001603 return( 128 );
1604 default:
1605 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001606 }
1607}
Gilles Peskinea05219c2018-11-16 16:02:56 +01001608#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001609
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001610/* Initialize the MAC operation structure. Once this function has been
1611 * called, psa_mac_abort can run and will do the right thing. */
1612static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1613 psa_algorithm_t alg )
1614{
1615 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1616
1617 operation->alg = alg;
1618 operation->key_set = 0;
1619 operation->iv_set = 0;
1620 operation->iv_required = 0;
1621 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001622 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001623
1624#if defined(MBEDTLS_CMAC_C)
1625 if( alg == PSA_ALG_CMAC )
1626 {
1627 operation->iv_required = 0;
1628 mbedtls_cipher_init( &operation->ctx.cmac );
1629 status = PSA_SUCCESS;
1630 }
1631 else
1632#endif /* MBEDTLS_CMAC_C */
1633#if defined(MBEDTLS_MD_C)
1634 if( PSA_ALG_IS_HMAC( operation->alg ) )
1635 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001636 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1637 operation->ctx.hmac.hash_ctx.alg = 0;
1638 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001639 }
1640 else
1641#endif /* MBEDTLS_MD_C */
1642 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001643 if( ! PSA_ALG_IS_MAC( alg ) )
1644 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001645 }
1646
1647 if( status != PSA_SUCCESS )
1648 memset( operation, 0, sizeof( *operation ) );
1649 return( status );
1650}
1651
Gilles Peskine01126fa2018-07-12 17:04:55 +02001652#if defined(MBEDTLS_MD_C)
1653static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1654{
Gilles Peskine3f108122018-12-07 18:14:53 +01001655 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001656 return( psa_hash_abort( &hmac->hash_ctx ) );
1657}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01001658
1659static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
1660{
1661 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
1662 memset( hmac, 0, sizeof( *hmac ) );
1663}
Gilles Peskine01126fa2018-07-12 17:04:55 +02001664#endif /* MBEDTLS_MD_C */
1665
Gilles Peskine8c9def32018-02-08 10:02:12 +01001666psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1667{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001668 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001669 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001670 /* The object has (apparently) been initialized but it is not
1671 * in use. It's ok to call abort on such an object, and there's
1672 * nothing to do. */
1673 return( PSA_SUCCESS );
1674 }
1675 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001676#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001677 if( operation->alg == PSA_ALG_CMAC )
1678 {
1679 mbedtls_cipher_free( &operation->ctx.cmac );
1680 }
1681 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001682#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001683#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001684 if( PSA_ALG_IS_HMAC( operation->alg ) )
1685 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001686 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001687 }
1688 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001689#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001690 {
1691 /* Sanity check (shouldn't happen: operation->alg should
1692 * always have been initialized to a valid value). */
1693 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001694 }
Moran Peker41deec42018-04-04 15:43:05 +03001695
Gilles Peskine8c9def32018-02-08 10:02:12 +01001696 operation->alg = 0;
1697 operation->key_set = 0;
1698 operation->iv_set = 0;
1699 operation->iv_required = 0;
1700 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001701 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001702
Gilles Peskine8c9def32018-02-08 10:02:12 +01001703 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001704
1705bad_state:
1706 /* If abort is called on an uninitialized object, we can't trust
1707 * anything. Wipe the object in case it contains confidential data.
1708 * This may result in a memory leak if a pointer gets overwritten,
1709 * but it's too late to do anything about this. */
1710 memset( operation, 0, sizeof( *operation ) );
1711 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001712}
1713
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001714#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001715static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001716 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001717 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001718 const mbedtls_cipher_info_t *cipher_info )
1719{
1720 int ret;
1721
1722 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001723
1724 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1725 if( ret != 0 )
1726 return( ret );
1727
1728 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1729 slot->data.raw.data,
1730 key_bits );
1731 return( ret );
1732}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001733#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001734
Gilles Peskine248051a2018-06-20 16:09:38 +02001735#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001736static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1737 const uint8_t *key,
1738 size_t key_length,
1739 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001740{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001741 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001742 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001743 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001744 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001745 psa_status_t status;
1746
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001747 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1748 * overflow below. This should never trigger if the hash algorithm
1749 * is implemented correctly. */
1750 /* The size checks against the ipad and opad buffers cannot be written
1751 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1752 * because that triggers -Wlogical-op on GCC 7.3. */
1753 if( block_size > sizeof( ipad ) )
1754 return( PSA_ERROR_NOT_SUPPORTED );
1755 if( block_size > sizeof( hmac->opad ) )
1756 return( PSA_ERROR_NOT_SUPPORTED );
1757 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001758 return( PSA_ERROR_NOT_SUPPORTED );
1759
Gilles Peskined223b522018-06-11 18:12:58 +02001760 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001761 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001762 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001763 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001764 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001765 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001766 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001767 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001768 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001769 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001770 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001771 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001772 }
Gilles Peskine96889972018-07-12 17:07:03 +02001773 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1774 * but it is permitted. It is common when HMAC is used in HKDF, for
1775 * example. Don't call `memcpy` in the 0-length because `key` could be
1776 * an invalid pointer which would make the behavior undefined. */
1777 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001778 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001779
Gilles Peskined223b522018-06-11 18:12:58 +02001780 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1781 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001782 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001783 ipad[i] ^= 0x36;
1784 memset( ipad + key_length, 0x36, block_size - key_length );
1785
1786 /* Copy the key material from ipad to opad, flipping the requisite bits,
1787 * and filling the rest of opad with the requisite constant. */
1788 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001789 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1790 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001791
Gilles Peskine01126fa2018-07-12 17:04:55 +02001792 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001793 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001794 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001795
Gilles Peskine01126fa2018-07-12 17:04:55 +02001796 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001797
1798cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01001799 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001800
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001801 return( status );
1802}
Gilles Peskine248051a2018-06-20 16:09:38 +02001803#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001804
Gilles Peskine89167cb2018-07-08 20:12:23 +02001805static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001806 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001807 psa_algorithm_t alg,
1808 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001809{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001810 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001811 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001812 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001813 psa_key_usage_t usage =
1814 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02001815 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02001816 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001817
Gilles Peskined911eb72018-08-14 15:18:45 +02001818 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001819 if( status != PSA_SUCCESS )
1820 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001821 if( is_sign )
1822 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001823
Gilles Peskinec5487a82018-12-03 18:08:14 +01001824 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001825 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001826 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001827 key_bits = psa_get_key_bits( slot );
1828
Gilles Peskine8c9def32018-02-08 10:02:12 +01001829#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001830 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001831 {
1832 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02001833 mbedtls_cipher_info_from_psa( full_length_alg,
1834 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001835 int ret;
1836 if( cipher_info == NULL )
1837 {
1838 status = PSA_ERROR_NOT_SUPPORTED;
1839 goto exit;
1840 }
1841 operation->mac_size = cipher_info->block_size;
1842 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
1843 status = mbedtls_to_psa_error( ret );
1844 }
1845 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001846#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001847#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001848 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001849 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02001850 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001851 if( hash_alg == 0 )
1852 {
1853 status = PSA_ERROR_NOT_SUPPORTED;
1854 goto exit;
1855 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001856
1857 operation->mac_size = PSA_HASH_SIZE( hash_alg );
1858 /* Sanity check. This shouldn't fail on a valid configuration. */
1859 if( operation->mac_size == 0 ||
1860 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
1861 {
1862 status = PSA_ERROR_NOT_SUPPORTED;
1863 goto exit;
1864 }
1865
Gilles Peskine01126fa2018-07-12 17:04:55 +02001866 if( slot->type != PSA_KEY_TYPE_HMAC )
1867 {
1868 status = PSA_ERROR_INVALID_ARGUMENT;
1869 goto exit;
1870 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001871
Gilles Peskine01126fa2018-07-12 17:04:55 +02001872 status = psa_hmac_setup_internal( &operation->ctx.hmac,
1873 slot->data.raw.data,
1874 slot->data.raw.bytes,
1875 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001876 }
1877 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001878#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001879 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00001880 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02001881 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001882 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001883
Gilles Peskined911eb72018-08-14 15:18:45 +02001884 if( truncated == 0 )
1885 {
1886 /* The "normal" case: untruncated algorithm. Nothing to do. */
1887 }
1888 else if( truncated < 4 )
1889 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02001890 /* A very short MAC is too short for security since it can be
1891 * brute-forced. Ancient protocols with 32-bit MACs do exist,
1892 * so we make this our minimum, even though 32 bits is still
1893 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02001894 status = PSA_ERROR_NOT_SUPPORTED;
1895 }
1896 else if( truncated > operation->mac_size )
1897 {
1898 /* It's impossible to "truncate" to a larger length. */
1899 status = PSA_ERROR_INVALID_ARGUMENT;
1900 }
1901 else
1902 operation->mac_size = truncated;
1903
Gilles Peskinefbfac682018-07-08 20:51:54 +02001904exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001905 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001906 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001907 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001908 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001909 else
1910 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001911 operation->key_set = 1;
1912 }
1913 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001914}
1915
Gilles Peskine89167cb2018-07-08 20:12:23 +02001916psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001917 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001918 psa_algorithm_t alg )
1919{
Gilles Peskinec5487a82018-12-03 18:08:14 +01001920 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001921}
1922
1923psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001924 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001925 psa_algorithm_t alg )
1926{
Gilles Peskinec5487a82018-12-03 18:08:14 +01001927 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001928}
1929
Gilles Peskine8c9def32018-02-08 10:02:12 +01001930psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1931 const uint8_t *input,
1932 size_t input_length )
1933{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001934 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001935 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001936 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001937 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001938 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001939 operation->has_input = 1;
1940
Gilles Peskine8c9def32018-02-08 10:02:12 +01001941#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001942 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001943 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001944 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1945 input, input_length );
1946 status = mbedtls_to_psa_error( ret );
1947 }
1948 else
1949#endif /* MBEDTLS_CMAC_C */
1950#if defined(MBEDTLS_MD_C)
1951 if( PSA_ALG_IS_HMAC( operation->alg ) )
1952 {
1953 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
1954 input_length );
1955 }
1956 else
1957#endif /* MBEDTLS_MD_C */
1958 {
1959 /* This shouldn't happen if `operation` was initialized by
1960 * a setup function. */
1961 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001962 }
1963
Gilles Peskinefbfac682018-07-08 20:51:54 +02001964cleanup:
1965 if( status != PSA_SUCCESS )
1966 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001967 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001968}
1969
Gilles Peskine01126fa2018-07-12 17:04:55 +02001970#if defined(MBEDTLS_MD_C)
1971static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
1972 uint8_t *mac,
1973 size_t mac_size )
1974{
1975 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
1976 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
1977 size_t hash_size = 0;
1978 size_t block_size = psa_get_hash_block_size( hash_alg );
1979 psa_status_t status;
1980
Gilles Peskine01126fa2018-07-12 17:04:55 +02001981 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1982 if( status != PSA_SUCCESS )
1983 return( status );
1984 /* From here on, tmp needs to be wiped. */
1985
1986 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
1987 if( status != PSA_SUCCESS )
1988 goto exit;
1989
1990 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
1991 if( status != PSA_SUCCESS )
1992 goto exit;
1993
1994 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
1995 if( status != PSA_SUCCESS )
1996 goto exit;
1997
Gilles Peskined911eb72018-08-14 15:18:45 +02001998 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1999 if( status != PSA_SUCCESS )
2000 goto exit;
2001
2002 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002003
2004exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002005 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002006 return( status );
2007}
2008#endif /* MBEDTLS_MD_C */
2009
mohammad16036df908f2018-04-02 08:34:15 -07002010static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002011 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002012 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002013{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002014 if( ! operation->key_set )
2015 return( PSA_ERROR_BAD_STATE );
2016 if( operation->iv_required && ! operation->iv_set )
2017 return( PSA_ERROR_BAD_STATE );
2018
Gilles Peskine8c9def32018-02-08 10:02:12 +01002019 if( mac_size < operation->mac_size )
2020 return( PSA_ERROR_BUFFER_TOO_SMALL );
2021
Gilles Peskine8c9def32018-02-08 10:02:12 +01002022#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002023 if( operation->alg == PSA_ALG_CMAC )
2024 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002025 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2026 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2027 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002028 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002029 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002030 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002031 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002032 else
2033#endif /* MBEDTLS_CMAC_C */
2034#if defined(MBEDTLS_MD_C)
2035 if( PSA_ALG_IS_HMAC( operation->alg ) )
2036 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002037 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002038 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002039 }
2040 else
2041#endif /* MBEDTLS_MD_C */
2042 {
2043 /* This shouldn't happen if `operation` was initialized by
2044 * a setup function. */
2045 return( PSA_ERROR_BAD_STATE );
2046 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002047}
2048
Gilles Peskineacd4be32018-07-08 19:56:25 +02002049psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2050 uint8_t *mac,
2051 size_t mac_size,
2052 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002053{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002054 psa_status_t status;
2055
2056 /* Fill the output buffer with something that isn't a valid mac
2057 * (barring an attack on the mac and deliberately-crafted input),
2058 * in case the caller doesn't check the return status properly. */
2059 *mac_length = mac_size;
2060 /* If mac_size is 0 then mac may be NULL and then the
2061 * call to memset would have undefined behavior. */
2062 if( mac_size != 0 )
2063 memset( mac, '!', mac_size );
2064
Gilles Peskine89167cb2018-07-08 20:12:23 +02002065 if( ! operation->is_sign )
2066 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002067 status = PSA_ERROR_BAD_STATE;
2068 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002069 }
mohammad16036df908f2018-04-02 08:34:15 -07002070
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002071 status = psa_mac_finish_internal( operation, mac, mac_size );
2072
2073cleanup:
2074 if( status == PSA_SUCCESS )
2075 {
2076 status = psa_mac_abort( operation );
2077 if( status == PSA_SUCCESS )
2078 *mac_length = operation->mac_size;
2079 else
2080 memset( mac, '!', mac_size );
2081 }
2082 else
2083 psa_mac_abort( operation );
2084 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002085}
2086
Gilles Peskineacd4be32018-07-08 19:56:25 +02002087psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2088 const uint8_t *mac,
2089 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002090{
Gilles Peskine828ed142018-06-18 23:25:51 +02002091 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002092 psa_status_t status;
2093
Gilles Peskine89167cb2018-07-08 20:12:23 +02002094 if( operation->is_sign )
2095 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002096 status = PSA_ERROR_BAD_STATE;
2097 goto cleanup;
2098 }
2099 if( operation->mac_size != mac_length )
2100 {
2101 status = PSA_ERROR_INVALID_SIGNATURE;
2102 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002103 }
mohammad16036df908f2018-04-02 08:34:15 -07002104
2105 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002106 actual_mac, sizeof( actual_mac ) );
2107
2108 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2109 status = PSA_ERROR_INVALID_SIGNATURE;
2110
2111cleanup:
2112 if( status == PSA_SUCCESS )
2113 status = psa_mac_abort( operation );
2114 else
2115 psa_mac_abort( operation );
2116
Gilles Peskine3f108122018-12-07 18:14:53 +01002117 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002118
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002119 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002120}
2121
2122
Gilles Peskine20035e32018-02-03 22:44:14 +01002123
Gilles Peskine20035e32018-02-03 22:44:14 +01002124/****************************************************************/
2125/* Asymmetric cryptography */
2126/****************************************************************/
2127
Gilles Peskine2b450e32018-06-27 15:42:46 +02002128#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002129/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002130 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002131static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2132 size_t hash_length,
2133 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002134{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002135 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002136 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002137 *md_alg = mbedtls_md_get_type( md_info );
2138
2139 /* The Mbed TLS RSA module uses an unsigned int for hash length
2140 * parameters. Validate that it fits so that we don't risk an
2141 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002142#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002143 if( hash_length > UINT_MAX )
2144 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002145#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002146
2147#if defined(MBEDTLS_PKCS1_V15)
2148 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2149 * must be correct. */
2150 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2151 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002152 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002153 if( md_info == NULL )
2154 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002155 if( mbedtls_md_get_size( md_info ) != hash_length )
2156 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002157 }
2158#endif /* MBEDTLS_PKCS1_V15 */
2159
2160#if defined(MBEDTLS_PKCS1_V21)
2161 /* PSS requires a hash internally. */
2162 if( PSA_ALG_IS_RSA_PSS( alg ) )
2163 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002164 if( md_info == NULL )
2165 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002166 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002167#endif /* MBEDTLS_PKCS1_V21 */
2168
Gilles Peskine61b91d42018-06-08 16:09:36 +02002169 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002170}
2171
Gilles Peskine2b450e32018-06-27 15:42:46 +02002172static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2173 psa_algorithm_t alg,
2174 const uint8_t *hash,
2175 size_t hash_length,
2176 uint8_t *signature,
2177 size_t signature_size,
2178 size_t *signature_length )
2179{
2180 psa_status_t status;
2181 int ret;
2182 mbedtls_md_type_t md_alg;
2183
2184 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2185 if( status != PSA_SUCCESS )
2186 return( status );
2187
Gilles Peskine630a18a2018-06-29 17:49:35 +02002188 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002189 return( PSA_ERROR_BUFFER_TOO_SMALL );
2190
2191#if defined(MBEDTLS_PKCS1_V15)
2192 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2193 {
2194 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2195 MBEDTLS_MD_NONE );
2196 ret = mbedtls_rsa_pkcs1_sign( rsa,
2197 mbedtls_ctr_drbg_random,
2198 &global_data.ctr_drbg,
2199 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002200 md_alg,
2201 (unsigned int) hash_length,
2202 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002203 signature );
2204 }
2205 else
2206#endif /* MBEDTLS_PKCS1_V15 */
2207#if defined(MBEDTLS_PKCS1_V21)
2208 if( PSA_ALG_IS_RSA_PSS( alg ) )
2209 {
2210 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2211 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2212 mbedtls_ctr_drbg_random,
2213 &global_data.ctr_drbg,
2214 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002215 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002216 (unsigned int) hash_length,
2217 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002218 signature );
2219 }
2220 else
2221#endif /* MBEDTLS_PKCS1_V21 */
2222 {
2223 return( PSA_ERROR_INVALID_ARGUMENT );
2224 }
2225
2226 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002227 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002228 return( mbedtls_to_psa_error( ret ) );
2229}
2230
2231static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2232 psa_algorithm_t alg,
2233 const uint8_t *hash,
2234 size_t hash_length,
2235 const uint8_t *signature,
2236 size_t signature_length )
2237{
2238 psa_status_t status;
2239 int ret;
2240 mbedtls_md_type_t md_alg;
2241
2242 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2243 if( status != PSA_SUCCESS )
2244 return( status );
2245
Gilles Peskine630a18a2018-06-29 17:49:35 +02002246 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002247 return( PSA_ERROR_BUFFER_TOO_SMALL );
2248
2249#if defined(MBEDTLS_PKCS1_V15)
2250 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2251 {
2252 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2253 MBEDTLS_MD_NONE );
2254 ret = mbedtls_rsa_pkcs1_verify( rsa,
2255 mbedtls_ctr_drbg_random,
2256 &global_data.ctr_drbg,
2257 MBEDTLS_RSA_PUBLIC,
2258 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002259 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002260 hash,
2261 signature );
2262 }
2263 else
2264#endif /* MBEDTLS_PKCS1_V15 */
2265#if defined(MBEDTLS_PKCS1_V21)
2266 if( PSA_ALG_IS_RSA_PSS( alg ) )
2267 {
2268 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2269 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2270 mbedtls_ctr_drbg_random,
2271 &global_data.ctr_drbg,
2272 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002273 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002274 (unsigned int) hash_length,
2275 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002276 signature );
2277 }
2278 else
2279#endif /* MBEDTLS_PKCS1_V21 */
2280 {
2281 return( PSA_ERROR_INVALID_ARGUMENT );
2282 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002283
2284 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2285 * the rest of the signature is invalid". This has little use in
2286 * practice and PSA doesn't report this distinction. */
2287 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2288 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002289 return( mbedtls_to_psa_error( ret ) );
2290}
2291#endif /* MBEDTLS_RSA_C */
2292
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002293#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002294/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2295 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2296 * (even though these functions don't modify it). */
2297static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2298 psa_algorithm_t alg,
2299 const uint8_t *hash,
2300 size_t hash_length,
2301 uint8_t *signature,
2302 size_t signature_size,
2303 size_t *signature_length )
2304{
2305 int ret;
2306 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002307 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002308 mbedtls_mpi_init( &r );
2309 mbedtls_mpi_init( &s );
2310
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002311 if( signature_size < 2 * curve_bytes )
2312 {
2313 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2314 goto cleanup;
2315 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002316
Gilles Peskinea05219c2018-11-16 16:02:56 +01002317#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002318 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2319 {
2320 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2321 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2322 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2323 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2324 hash, hash_length,
2325 md_alg ) );
2326 }
2327 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002328#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002329 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002330 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002331 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2332 hash, hash_length,
2333 mbedtls_ctr_drbg_random,
2334 &global_data.ctr_drbg ) );
2335 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002336
2337 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2338 signature,
2339 curve_bytes ) );
2340 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2341 signature + curve_bytes,
2342 curve_bytes ) );
2343
2344cleanup:
2345 mbedtls_mpi_free( &r );
2346 mbedtls_mpi_free( &s );
2347 if( ret == 0 )
2348 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002349 return( mbedtls_to_psa_error( ret ) );
2350}
2351
2352static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2353 const uint8_t *hash,
2354 size_t hash_length,
2355 const uint8_t *signature,
2356 size_t signature_length )
2357{
2358 int ret;
2359 mbedtls_mpi r, s;
2360 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2361 mbedtls_mpi_init( &r );
2362 mbedtls_mpi_init( &s );
2363
2364 if( signature_length != 2 * curve_bytes )
2365 return( PSA_ERROR_INVALID_SIGNATURE );
2366
2367 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2368 signature,
2369 curve_bytes ) );
2370 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2371 signature + curve_bytes,
2372 curve_bytes ) );
2373
2374 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2375 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002376
2377cleanup:
2378 mbedtls_mpi_free( &r );
2379 mbedtls_mpi_free( &s );
2380 return( mbedtls_to_psa_error( ret ) );
2381}
2382#endif /* MBEDTLS_ECDSA_C */
2383
Gilles Peskinec5487a82018-12-03 18:08:14 +01002384psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002385 psa_algorithm_t alg,
2386 const uint8_t *hash,
2387 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002388 uint8_t *signature,
2389 size_t signature_size,
2390 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002391{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002392 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002393 psa_status_t status;
2394
2395 *signature_length = signature_size;
2396
Gilles Peskinec5487a82018-12-03 18:08:14 +01002397 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002398 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002399 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002400 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002401 {
2402 status = PSA_ERROR_INVALID_ARGUMENT;
2403 goto exit;
2404 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002405
Gilles Peskine20035e32018-02-03 22:44:14 +01002406#if defined(MBEDTLS_RSA_C)
2407 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2408 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002409 status = psa_rsa_sign( slot->data.rsa,
2410 alg,
2411 hash, hash_length,
2412 signature, signature_size,
2413 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002414 }
2415 else
2416#endif /* defined(MBEDTLS_RSA_C) */
2417#if defined(MBEDTLS_ECP_C)
2418 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2419 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002420#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002421 if(
2422#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2423 PSA_ALG_IS_ECDSA( alg )
2424#else
2425 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2426#endif
2427 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002428 status = psa_ecdsa_sign( slot->data.ecp,
2429 alg,
2430 hash, hash_length,
2431 signature, signature_size,
2432 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002433 else
2434#endif /* defined(MBEDTLS_ECDSA_C) */
2435 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002436 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002437 }
itayzafrir5c753392018-05-08 11:18:38 +03002438 }
2439 else
2440#endif /* defined(MBEDTLS_ECP_C) */
2441 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002442 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002443 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002444
2445exit:
2446 /* Fill the unused part of the output buffer (the whole buffer on error,
2447 * the trailing part on success) with something that isn't a valid mac
2448 * (barring an attack on the mac and deliberately-crafted input),
2449 * in case the caller doesn't check the return status properly. */
2450 if( status == PSA_SUCCESS )
2451 memset( signature + *signature_length, '!',
2452 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002453 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002454 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002455 /* If signature_size is 0 then we have nothing to do. We must not call
2456 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002457 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002458}
2459
Gilles Peskinec5487a82018-12-03 18:08:14 +01002460psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03002461 psa_algorithm_t alg,
2462 const uint8_t *hash,
2463 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002464 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002465 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002466{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002467 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002468 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002469
Gilles Peskinec5487a82018-12-03 18:08:14 +01002470 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002471 if( status != PSA_SUCCESS )
2472 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002473
Gilles Peskine61b91d42018-06-08 16:09:36 +02002474#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002475 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002476 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002477 return( psa_rsa_verify( slot->data.rsa,
2478 alg,
2479 hash, hash_length,
2480 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002481 }
2482 else
2483#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002484#if defined(MBEDTLS_ECP_C)
2485 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2486 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002487#if defined(MBEDTLS_ECDSA_C)
2488 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002489 return( psa_ecdsa_verify( slot->data.ecp,
2490 hash, hash_length,
2491 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002492 else
2493#endif /* defined(MBEDTLS_ECDSA_C) */
2494 {
2495 return( PSA_ERROR_INVALID_ARGUMENT );
2496 }
itayzafrir5c753392018-05-08 11:18:38 +03002497 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002498 else
2499#endif /* defined(MBEDTLS_ECP_C) */
2500 {
2501 return( PSA_ERROR_NOT_SUPPORTED );
2502 }
2503}
2504
Gilles Peskine072ac562018-06-30 00:21:29 +02002505#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2506static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2507 mbedtls_rsa_context *rsa )
2508{
2509 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2510 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2511 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2512 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2513}
2514#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2515
Gilles Peskinec5487a82018-12-03 18:08:14 +01002516psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002517 psa_algorithm_t alg,
2518 const uint8_t *input,
2519 size_t input_length,
2520 const uint8_t *salt,
2521 size_t salt_length,
2522 uint8_t *output,
2523 size_t output_size,
2524 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002525{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002526 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002527 psa_status_t status;
2528
Darryl Green5cc689a2018-07-24 15:34:10 +01002529 (void) input;
2530 (void) input_length;
2531 (void) salt;
2532 (void) output;
2533 (void) output_size;
2534
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002535 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002536
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002537 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2538 return( PSA_ERROR_INVALID_ARGUMENT );
2539
Gilles Peskinec5487a82018-12-03 18:08:14 +01002540 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002541 if( status != PSA_SUCCESS )
2542 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002543 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2544 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002545 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002546
2547#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002548 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002549 {
2550 mbedtls_rsa_context *rsa = slot->data.rsa;
2551 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002552 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002553 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002554#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002555 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002556 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002557 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2558 mbedtls_ctr_drbg_random,
2559 &global_data.ctr_drbg,
2560 MBEDTLS_RSA_PUBLIC,
2561 input_length,
2562 input,
2563 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002564 }
2565 else
2566#endif /* MBEDTLS_PKCS1_V15 */
2567#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002568 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002569 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002570 psa_rsa_oaep_set_padding_mode( alg, rsa );
2571 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2572 mbedtls_ctr_drbg_random,
2573 &global_data.ctr_drbg,
2574 MBEDTLS_RSA_PUBLIC,
2575 salt, salt_length,
2576 input_length,
2577 input,
2578 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002579 }
2580 else
2581#endif /* MBEDTLS_PKCS1_V21 */
2582 {
2583 return( PSA_ERROR_INVALID_ARGUMENT );
2584 }
2585 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002586 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002587 return( mbedtls_to_psa_error( ret ) );
2588 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002589 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002590#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002591 {
2592 return( PSA_ERROR_NOT_SUPPORTED );
2593 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002594}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002595
Gilles Peskinec5487a82018-12-03 18:08:14 +01002596psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002597 psa_algorithm_t alg,
2598 const uint8_t *input,
2599 size_t input_length,
2600 const uint8_t *salt,
2601 size_t salt_length,
2602 uint8_t *output,
2603 size_t output_size,
2604 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002605{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002606 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002607 psa_status_t status;
2608
Darryl Green5cc689a2018-07-24 15:34:10 +01002609 (void) input;
2610 (void) input_length;
2611 (void) salt;
2612 (void) output;
2613 (void) output_size;
2614
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002615 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002616
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002617 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2618 return( PSA_ERROR_INVALID_ARGUMENT );
2619
Gilles Peskinec5487a82018-12-03 18:08:14 +01002620 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002621 if( status != PSA_SUCCESS )
2622 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002623 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2624 return( PSA_ERROR_INVALID_ARGUMENT );
2625
2626#if defined(MBEDTLS_RSA_C)
2627 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2628 {
2629 mbedtls_rsa_context *rsa = slot->data.rsa;
2630 int ret;
2631
Gilles Peskine630a18a2018-06-29 17:49:35 +02002632 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002633 return( PSA_ERROR_INVALID_ARGUMENT );
2634
2635#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002636 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002637 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002638 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2639 mbedtls_ctr_drbg_random,
2640 &global_data.ctr_drbg,
2641 MBEDTLS_RSA_PRIVATE,
2642 output_length,
2643 input,
2644 output,
2645 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002646 }
2647 else
2648#endif /* MBEDTLS_PKCS1_V15 */
2649#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002650 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002651 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002652 psa_rsa_oaep_set_padding_mode( alg, rsa );
2653 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2654 mbedtls_ctr_drbg_random,
2655 &global_data.ctr_drbg,
2656 MBEDTLS_RSA_PRIVATE,
2657 salt, salt_length,
2658 output_length,
2659 input,
2660 output,
2661 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002662 }
2663 else
2664#endif /* MBEDTLS_PKCS1_V21 */
2665 {
2666 return( PSA_ERROR_INVALID_ARGUMENT );
2667 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002668
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002669 return( mbedtls_to_psa_error( ret ) );
2670 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002671 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002672#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002673 {
2674 return( PSA_ERROR_NOT_SUPPORTED );
2675 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002676}
Gilles Peskine20035e32018-02-03 22:44:14 +01002677
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002678
2679
mohammad1603503973b2018-03-12 15:59:30 +02002680/****************************************************************/
2681/* Symmetric cryptography */
2682/****************************************************************/
2683
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002684/* Initialize the cipher operation structure. Once this function has been
2685 * called, psa_cipher_abort can run and will do the right thing. */
2686static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2687 psa_algorithm_t alg )
2688{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002689 if( ! PSA_ALG_IS_CIPHER( alg ) )
2690 {
2691 memset( operation, 0, sizeof( *operation ) );
2692 return( PSA_ERROR_INVALID_ARGUMENT );
2693 }
2694
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002695 operation->alg = alg;
2696 operation->key_set = 0;
2697 operation->iv_set = 0;
2698 operation->iv_required = 1;
2699 operation->iv_size = 0;
2700 operation->block_size = 0;
2701 mbedtls_cipher_init( &operation->ctx.cipher );
2702 return( PSA_SUCCESS );
2703}
2704
Gilles Peskinee553c652018-06-04 16:22:46 +02002705static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002706 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02002707 psa_algorithm_t alg,
2708 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002709{
2710 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2711 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002712 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002713 size_t key_bits;
2714 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002715 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2716 PSA_KEY_USAGE_ENCRYPT :
2717 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002718
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002719 status = psa_cipher_init( operation, alg );
2720 if( status != PSA_SUCCESS )
2721 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002722
Gilles Peskinec5487a82018-12-03 18:08:14 +01002723 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002724 if( status != PSA_SUCCESS )
2725 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002726 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002727
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002728 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002729 if( cipher_info == NULL )
2730 return( PSA_ERROR_NOT_SUPPORTED );
2731
mohammad1603503973b2018-03-12 15:59:30 +02002732 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002733 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002734 {
2735 psa_cipher_abort( operation );
2736 return( mbedtls_to_psa_error( ret ) );
2737 }
2738
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002739#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002740 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002741 {
2742 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2743 unsigned char keys[24];
2744 memcpy( keys, slot->data.raw.data, 16 );
2745 memcpy( keys + 16, slot->data.raw.data, 8 );
2746 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2747 keys,
2748 192, cipher_operation );
2749 }
2750 else
2751#endif
2752 {
2753 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2754 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002755 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002756 }
Moran Peker41deec42018-04-04 15:43:05 +03002757 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002758 {
2759 psa_cipher_abort( operation );
2760 return( mbedtls_to_psa_error( ret ) );
2761 }
2762
mohammad16038481e742018-03-18 13:57:31 +02002763#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002764 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002765 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002766 case PSA_ALG_CBC_NO_PADDING:
2767 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2768 MBEDTLS_PADDING_NONE );
2769 break;
2770 case PSA_ALG_CBC_PKCS7:
2771 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2772 MBEDTLS_PADDING_PKCS7 );
2773 break;
2774 default:
2775 /* The algorithm doesn't involve padding. */
2776 ret = 0;
2777 break;
2778 }
2779 if( ret != 0 )
2780 {
2781 psa_cipher_abort( operation );
2782 return( mbedtls_to_psa_error( ret ) );
mohammad16038481e742018-03-18 13:57:31 +02002783 }
2784#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2785
mohammad1603503973b2018-03-12 15:59:30 +02002786 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002787 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2788 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
2789 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02002790 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002791 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002792 }
mohammad1603503973b2018-03-12 15:59:30 +02002793
Moran Peker395db872018-05-31 14:07:14 +03002794 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002795}
2796
Gilles Peskinefe119512018-07-08 21:39:34 +02002797psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002798 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02002799 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002800{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002801 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002802}
2803
Gilles Peskinefe119512018-07-08 21:39:34 +02002804psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002805 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02002806 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002807{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002808 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002809}
2810
Gilles Peskinefe119512018-07-08 21:39:34 +02002811psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2812 unsigned char *iv,
2813 size_t iv_size,
2814 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002815{
itayzafrir534bd7c2018-08-02 13:56:32 +03002816 psa_status_t status;
2817 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002818 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002819 {
2820 status = PSA_ERROR_BAD_STATE;
2821 goto exit;
2822 }
Moran Peker41deec42018-04-04 15:43:05 +03002823 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002824 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002825 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03002826 goto exit;
2827 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002828 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2829 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002830 if( ret != 0 )
2831 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002832 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002833 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002834 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002835
mohammad16038481e742018-03-18 13:57:31 +02002836 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03002837 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002838
Moran Peker395db872018-05-31 14:07:14 +03002839exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03002840 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03002841 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002842 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002843}
2844
Gilles Peskinefe119512018-07-08 21:39:34 +02002845psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2846 const unsigned char *iv,
2847 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002848{
itayzafrir534bd7c2018-08-02 13:56:32 +03002849 psa_status_t status;
2850 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002851 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002852 {
2853 status = PSA_ERROR_BAD_STATE;
2854 goto exit;
2855 }
Moran Pekera28258c2018-05-29 16:25:04 +03002856 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002857 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002858 status = PSA_ERROR_INVALID_ARGUMENT;
2859 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03002860 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002861 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
2862 status = mbedtls_to_psa_error( ret );
2863exit:
2864 if( status == PSA_SUCCESS )
2865 operation->iv_set = 1;
2866 else
mohammad1603503973b2018-03-12 15:59:30 +02002867 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002868 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002869}
2870
Gilles Peskinee553c652018-06-04 16:22:46 +02002871psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2872 const uint8_t *input,
2873 size_t input_length,
2874 unsigned char *output,
2875 size_t output_size,
2876 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002877{
itayzafrir534bd7c2018-08-02 13:56:32 +03002878 psa_status_t status;
2879 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002880 size_t expected_output_size;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002881 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002882 {
2883 /* Take the unprocessed partial block left over from previous
2884 * update calls, if any, plus the input to this call. Remove
2885 * the last partial block, if any. You get the data that will be
2886 * output in this call. */
2887 expected_output_size =
2888 ( operation->ctx.cipher.unprocessed_len + input_length )
2889 / operation->block_size * operation->block_size;
2890 }
2891 else
2892 {
2893 expected_output_size = input_length;
2894 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002895
Gilles Peskine89d789c2018-06-04 17:17:16 +02002896 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03002897 {
2898 status = PSA_ERROR_BUFFER_TOO_SMALL;
2899 goto exit;
2900 }
mohammad160382759612018-03-12 18:16:40 +02002901
mohammad1603503973b2018-03-12 15:59:30 +02002902 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002903 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03002904 status = mbedtls_to_psa_error( ret );
2905exit:
2906 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02002907 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002908 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002909}
2910
Gilles Peskinee553c652018-06-04 16:22:46 +02002911psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2912 uint8_t *output,
2913 size_t output_size,
2914 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002915{
Janos Follath315b51c2018-07-09 16:04:51 +01002916 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
2917 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002918 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002919
mohammad1603503973b2018-03-12 15:59:30 +02002920 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002921 {
Janos Follath315b51c2018-07-09 16:04:51 +01002922 status = PSA_ERROR_BAD_STATE;
2923 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002924 }
2925 if( operation->iv_required && ! operation->iv_set )
2926 {
Janos Follath315b51c2018-07-09 16:04:51 +01002927 status = PSA_ERROR_BAD_STATE;
2928 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002929 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002930
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002931 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002932 operation->alg == PSA_ALG_CBC_NO_PADDING &&
2933 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03002934 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002935 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01002936 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002937 }
2938
Janos Follath315b51c2018-07-09 16:04:51 +01002939 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
2940 temp_output_buffer,
2941 output_length );
2942 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002943 {
Janos Follath315b51c2018-07-09 16:04:51 +01002944 status = mbedtls_to_psa_error( cipher_ret );
2945 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02002946 }
Janos Follath315b51c2018-07-09 16:04:51 +01002947
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002948 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01002949 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002950 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002951 memcpy( output, temp_output_buffer, *output_length );
2952 else
2953 {
Janos Follath315b51c2018-07-09 16:04:51 +01002954 status = PSA_ERROR_BUFFER_TOO_SMALL;
2955 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002956 }
mohammad1603503973b2018-03-12 15:59:30 +02002957
Gilles Peskine3f108122018-12-07 18:14:53 +01002958 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002959 status = psa_cipher_abort( operation );
2960
2961 return( status );
2962
2963error:
2964
2965 *output_length = 0;
2966
Gilles Peskine3f108122018-12-07 18:14:53 +01002967 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002968 (void) psa_cipher_abort( operation );
2969
2970 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002971}
2972
Gilles Peskinee553c652018-06-04 16:22:46 +02002973psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2974{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002975 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02002976 {
2977 /* The object has (apparently) been initialized but it is not
2978 * in use. It's ok to call abort on such an object, and there's
2979 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002980 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02002981 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002982
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002983 /* Sanity check (shouldn't happen: operation->alg should
2984 * always have been initialized to a valid value). */
2985 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
2986 return( PSA_ERROR_BAD_STATE );
2987
mohammad1603503973b2018-03-12 15:59:30 +02002988 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002989
Moran Peker41deec42018-04-04 15:43:05 +03002990 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002991 operation->key_set = 0;
2992 operation->iv_set = 0;
2993 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002994 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002995 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002996
Moran Peker395db872018-05-31 14:07:14 +03002997 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002998}
2999
Gilles Peskinea0655c32018-04-30 17:06:50 +02003000
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003001
mohammad16038cc1cee2018-03-28 01:21:33 +03003002/****************************************************************/
3003/* Key Policy */
3004/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003005
mohammad160327010052018-07-03 13:16:15 +03003006#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02003007void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003008{
Gilles Peskine803ce742018-06-18 16:07:14 +02003009 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03003010}
3011
Gilles Peskine2d277862018-06-18 15:41:12 +02003012void psa_key_policy_set_usage( psa_key_policy_t *policy,
3013 psa_key_usage_t usage,
3014 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03003015{
mohammad16034eed7572018-03-28 05:14:59 -07003016 policy->usage = usage;
3017 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03003018}
3019
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003020psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003021{
mohammad16036df908f2018-04-02 08:34:15 -07003022 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03003023}
3024
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003025psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003026{
mohammad16036df908f2018-04-02 08:34:15 -07003027 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03003028}
mohammad160327010052018-07-03 13:16:15 +03003029#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003030
Gilles Peskinec5487a82018-12-03 18:08:14 +01003031psa_status_t psa_set_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003032 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003033{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003034 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003035 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003036
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003037 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003038 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003039
Gilles Peskinec5487a82018-12-03 18:08:14 +01003040 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003041 if( status != PSA_SUCCESS )
3042 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03003043
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003044 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
3045 PSA_KEY_USAGE_ENCRYPT |
3046 PSA_KEY_USAGE_DECRYPT |
3047 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02003048 PSA_KEY_USAGE_VERIFY |
3049 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07003050 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03003051
mohammad16036df908f2018-04-02 08:34:15 -07003052 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003053
3054 return( PSA_SUCCESS );
3055}
3056
Gilles Peskinec5487a82018-12-03 18:08:14 +01003057psa_status_t psa_get_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003058 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003059{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003060 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003061 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003062
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003063 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003064 return( PSA_ERROR_INVALID_ARGUMENT );
3065
Gilles Peskinec5487a82018-12-03 18:08:14 +01003066 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003067 if( status != PSA_SUCCESS )
3068 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003069
mohammad16036df908f2018-04-02 08:34:15 -07003070 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003071
3072 return( PSA_SUCCESS );
3073}
Gilles Peskine20035e32018-02-03 22:44:14 +01003074
Gilles Peskinea0655c32018-04-30 17:06:50 +02003075
3076
mohammad1603804cd712018-03-20 22:44:08 +02003077/****************************************************************/
3078/* Key Lifetime */
3079/****************************************************************/
3080
Gilles Peskinec5487a82018-12-03 18:08:14 +01003081psa_status_t psa_get_key_lifetime( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003082 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003083{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003084 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003085 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003086
Gilles Peskinec5487a82018-12-03 18:08:14 +01003087 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003088 if( status != PSA_SUCCESS )
3089 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003090
mohammad1603804cd712018-03-20 22:44:08 +02003091 *lifetime = slot->lifetime;
3092
3093 return( PSA_SUCCESS );
3094}
3095
Gilles Peskine20035e32018-02-03 22:44:14 +01003096
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003097
mohammad16035955c982018-04-26 00:53:03 +03003098/****************************************************************/
3099/* AEAD */
3100/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003101
Gilles Peskineedf9a652018-08-17 18:11:56 +02003102typedef struct
3103{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003104 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003105 const mbedtls_cipher_info_t *cipher_info;
3106 union
3107 {
3108#if defined(MBEDTLS_CCM_C)
3109 mbedtls_ccm_context ccm;
3110#endif /* MBEDTLS_CCM_C */
3111#if defined(MBEDTLS_GCM_C)
3112 mbedtls_gcm_context gcm;
3113#endif /* MBEDTLS_GCM_C */
3114 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003115 psa_algorithm_t core_alg;
3116 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003117 uint8_t tag_length;
3118} aead_operation_t;
3119
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003120static void psa_aead_abort( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003121{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003122 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003123 {
3124#if defined(MBEDTLS_CCM_C)
3125 case PSA_ALG_CCM:
3126 mbedtls_ccm_free( &operation->ctx.ccm );
3127 break;
3128#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003129#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003130 case PSA_ALG_GCM:
3131 mbedtls_gcm_free( &operation->ctx.gcm );
3132 break;
3133#endif /* MBEDTLS_GCM_C */
3134 }
3135}
3136
3137static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003138 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003139 psa_key_usage_t usage,
3140 psa_algorithm_t alg )
3141{
3142 psa_status_t status;
3143 size_t key_bits;
3144 mbedtls_cipher_id_t cipher_id;
3145
Gilles Peskinec5487a82018-12-03 18:08:14 +01003146 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003147 if( status != PSA_SUCCESS )
3148 return( status );
3149
3150 key_bits = psa_get_key_bits( operation->slot );
3151
3152 operation->cipher_info =
3153 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3154 &cipher_id );
3155 if( operation->cipher_info == NULL )
3156 return( PSA_ERROR_NOT_SUPPORTED );
3157
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003158 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003159 {
3160#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003161 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3162 operation->core_alg = PSA_ALG_CCM;
3163 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003164 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3165 return( PSA_ERROR_INVALID_ARGUMENT );
3166 mbedtls_ccm_init( &operation->ctx.ccm );
3167 status = mbedtls_to_psa_error(
3168 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3169 operation->slot->data.raw.data,
3170 (unsigned int) key_bits ) );
3171 if( status != 0 )
3172 goto cleanup;
3173 break;
3174#endif /* MBEDTLS_CCM_C */
3175
3176#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003177 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3178 operation->core_alg = PSA_ALG_GCM;
3179 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003180 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3181 return( PSA_ERROR_INVALID_ARGUMENT );
3182 mbedtls_gcm_init( &operation->ctx.gcm );
3183 status = mbedtls_to_psa_error(
3184 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3185 operation->slot->data.raw.data,
3186 (unsigned int) key_bits ) );
3187 break;
3188#endif /* MBEDTLS_GCM_C */
3189
3190 default:
3191 return( PSA_ERROR_NOT_SUPPORTED );
3192 }
3193
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003194 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3195 {
3196 status = PSA_ERROR_INVALID_ARGUMENT;
3197 goto cleanup;
3198 }
3199 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3200 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3201 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3202 * In both cases, mbedtls_xxx will validate the tag length below. */
3203
Gilles Peskineedf9a652018-08-17 18:11:56 +02003204 return( PSA_SUCCESS );
3205
3206cleanup:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003207 psa_aead_abort( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003208 return( status );
3209}
3210
Gilles Peskinec5487a82018-12-03 18:08:14 +01003211psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003212 psa_algorithm_t alg,
3213 const uint8_t *nonce,
3214 size_t nonce_length,
3215 const uint8_t *additional_data,
3216 size_t additional_data_length,
3217 const uint8_t *plaintext,
3218 size_t plaintext_length,
3219 uint8_t *ciphertext,
3220 size_t ciphertext_size,
3221 size_t *ciphertext_length )
3222{
mohammad16035955c982018-04-26 00:53:03 +03003223 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003224 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003225 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003226
mohammad1603f08a5502018-06-03 15:05:47 +03003227 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003228
Gilles Peskinec5487a82018-12-03 18:08:14 +01003229 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003230 if( status != PSA_SUCCESS )
3231 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003232
Gilles Peskineedf9a652018-08-17 18:11:56 +02003233 /* For all currently supported modes, the tag is at the end of the
3234 * ciphertext. */
3235 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3236 {
3237 status = PSA_ERROR_BUFFER_TOO_SMALL;
3238 goto exit;
3239 }
3240 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003241
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003242#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003243 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003244 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003245 status = mbedtls_to_psa_error(
3246 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3247 MBEDTLS_GCM_ENCRYPT,
3248 plaintext_length,
3249 nonce, nonce_length,
3250 additional_data, additional_data_length,
3251 plaintext, ciphertext,
3252 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003253 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003254 else
3255#endif /* MBEDTLS_GCM_C */
3256#if defined(MBEDTLS_CCM_C)
3257 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003258 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003259 status = mbedtls_to_psa_error(
3260 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3261 plaintext_length,
3262 nonce, nonce_length,
3263 additional_data,
3264 additional_data_length,
3265 plaintext, ciphertext,
3266 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003267 }
mohammad16035c8845f2018-05-09 05:40:09 -07003268 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003269#endif /* MBEDTLS_CCM_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003270 {
mohammad1603554faad2018-06-03 15:07:38 +03003271 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003272 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003273
Gilles Peskineedf9a652018-08-17 18:11:56 +02003274 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3275 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003276
Gilles Peskineedf9a652018-08-17 18:11:56 +02003277exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003278 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003279 if( status == PSA_SUCCESS )
3280 *ciphertext_length = plaintext_length + operation.tag_length;
3281 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003282}
3283
Gilles Peskineee652a32018-06-01 19:23:52 +02003284/* Locate the tag in a ciphertext buffer containing the encrypted data
3285 * followed by the tag. Return the length of the part preceding the tag in
3286 * *plaintext_length. This is the size of the plaintext in modes where
3287 * the encrypted data has the same size as the plaintext, such as
3288 * CCM and GCM. */
3289static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3290 const uint8_t *ciphertext,
3291 size_t ciphertext_length,
3292 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003293 const uint8_t **p_tag )
3294{
3295 size_t payload_length;
3296 if( tag_length > ciphertext_length )
3297 return( PSA_ERROR_INVALID_ARGUMENT );
3298 payload_length = ciphertext_length - tag_length;
3299 if( payload_length > plaintext_size )
3300 return( PSA_ERROR_BUFFER_TOO_SMALL );
3301 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003302 return( PSA_SUCCESS );
3303}
3304
Gilles Peskinec5487a82018-12-03 18:08:14 +01003305psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003306 psa_algorithm_t alg,
3307 const uint8_t *nonce,
3308 size_t nonce_length,
3309 const uint8_t *additional_data,
3310 size_t additional_data_length,
3311 const uint8_t *ciphertext,
3312 size_t ciphertext_length,
3313 uint8_t *plaintext,
3314 size_t plaintext_size,
3315 size_t *plaintext_length )
3316{
mohammad16035955c982018-04-26 00:53:03 +03003317 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003318 aead_operation_t operation;
3319 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003320
Gilles Peskineee652a32018-06-01 19:23:52 +02003321 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003322
Gilles Peskinec5487a82018-12-03 18:08:14 +01003323 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003324 if( status != PSA_SUCCESS )
3325 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003326
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003327#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003328 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003329 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003330 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003331 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003332 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003333 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003334 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003335
Gilles Peskineedf9a652018-08-17 18:11:56 +02003336 status = mbedtls_to_psa_error(
3337 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3338 ciphertext_length - operation.tag_length,
3339 nonce, nonce_length,
3340 additional_data,
3341 additional_data_length,
3342 tag, operation.tag_length,
3343 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003344 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003345 else
3346#endif /* MBEDTLS_GCM_C */
3347#if defined(MBEDTLS_CCM_C)
3348 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003349 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003350 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003351 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003352 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003353 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003354 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003355
Gilles Peskineedf9a652018-08-17 18:11:56 +02003356 status = mbedtls_to_psa_error(
3357 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3358 ciphertext_length - operation.tag_length,
3359 nonce, nonce_length,
3360 additional_data,
3361 additional_data_length,
3362 ciphertext, plaintext,
3363 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003364 }
mohammad160339574652018-06-01 04:39:53 -07003365 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003366#endif /* MBEDTLS_CCM_C */
mohammad160339574652018-06-01 04:39:53 -07003367 {
mohammad1603554faad2018-06-03 15:07:38 +03003368 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003369 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003370
Gilles Peskineedf9a652018-08-17 18:11:56 +02003371 if( status != PSA_SUCCESS && plaintext_size != 0 )
3372 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003373
Gilles Peskineedf9a652018-08-17 18:11:56 +02003374exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003375 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003376 if( status == PSA_SUCCESS )
3377 *plaintext_length = ciphertext_length - operation.tag_length;
3378 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003379}
3380
Gilles Peskinea0655c32018-04-30 17:06:50 +02003381
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003382
Gilles Peskine20035e32018-02-03 22:44:14 +01003383/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003384/* Generators */
3385/****************************************************************/
3386
3387psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3388{
3389 psa_status_t status = PSA_SUCCESS;
3390 if( generator->alg == 0 )
3391 {
3392 /* The object has (apparently) been initialized but it is not
3393 * in use. It's ok to call abort on such an object, and there's
3394 * nothing to do. */
3395 }
3396 else
Gilles Peskine751d9652018-09-18 12:05:44 +02003397 if( generator->alg == PSA_ALG_SELECT_RAW )
3398 {
3399 if( generator->ctx.buffer.data != NULL )
3400 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003401 mbedtls_platform_zeroize( generator->ctx.buffer.data,
Gilles Peskine751d9652018-09-18 12:05:44 +02003402 generator->ctx.buffer.size );
3403 mbedtls_free( generator->ctx.buffer.data );
3404 }
3405 }
3406 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003407#if defined(MBEDTLS_MD_C)
3408 if( PSA_ALG_IS_HKDF( generator->alg ) )
3409 {
3410 mbedtls_free( generator->ctx.hkdf.info );
3411 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3412 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003413 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3414 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
3415 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003416 {
3417 if( generator->ctx.tls12_prf.key != NULL )
3418 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003419 mbedtls_platform_zeroize( generator->ctx.tls12_prf.key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003420 generator->ctx.tls12_prf.key_len );
3421 mbedtls_free( generator->ctx.tls12_prf.key );
3422 }
Hanno Becker580fba12018-11-13 20:50:45 +00003423
3424 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
3425 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003426 mbedtls_platform_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003427 generator->ctx.tls12_prf.Ai_with_seed_len );
3428 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
3429 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003430 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003431 else
3432#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003433 {
3434 status = PSA_ERROR_BAD_STATE;
3435 }
3436 memset( generator, 0, sizeof( *generator ) );
3437 return( status );
3438}
3439
3440
3441psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3442 size_t *capacity)
3443{
3444 *capacity = generator->capacity;
3445 return( PSA_SUCCESS );
3446}
3447
Gilles Peskinebef7f142018-07-12 17:22:21 +02003448#if defined(MBEDTLS_MD_C)
3449/* Read some bytes from an HKDF-based generator. This performs a chunk
3450 * of the expand phase of the HKDF algorithm. */
3451static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3452 psa_algorithm_t hash_alg,
3453 uint8_t *output,
3454 size_t output_length )
3455{
3456 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3457 psa_status_t status;
3458
3459 while( output_length != 0 )
3460 {
3461 /* Copy what remains of the current block */
3462 uint8_t n = hash_length - hkdf->offset_in_block;
3463 if( n > output_length )
3464 n = (uint8_t) output_length;
3465 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3466 output += n;
3467 output_length -= n;
3468 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003469 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003470 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003471 /* We can't be wanting more output after block 0xff, otherwise
3472 * the capacity check in psa_generator_read() would have
3473 * prevented this call. It could happen only if the generator
3474 * object was corrupted or if this function is called directly
3475 * inside the library. */
3476 if( hkdf->block_number == 0xff )
3477 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003478
3479 /* We need a new block */
3480 ++hkdf->block_number;
3481 hkdf->offset_in_block = 0;
3482 status = psa_hmac_setup_internal( &hkdf->hmac,
3483 hkdf->prk, hash_length,
3484 hash_alg );
3485 if( status != PSA_SUCCESS )
3486 return( status );
3487 if( hkdf->block_number != 1 )
3488 {
3489 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3490 hkdf->output_block,
3491 hash_length );
3492 if( status != PSA_SUCCESS )
3493 return( status );
3494 }
3495 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3496 hkdf->info,
3497 hkdf->info_length );
3498 if( status != PSA_SUCCESS )
3499 return( status );
3500 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3501 &hkdf->block_number, 1 );
3502 if( status != PSA_SUCCESS )
3503 return( status );
3504 status = psa_hmac_finish_internal( &hkdf->hmac,
3505 hkdf->output_block,
3506 sizeof( hkdf->output_block ) );
3507 if( status != PSA_SUCCESS )
3508 return( status );
3509 }
3510
3511 return( PSA_SUCCESS );
3512}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003513
3514static psa_status_t psa_generator_tls12_prf_generate_next_block(
3515 psa_tls12_prf_generator_t *tls12_prf,
3516 psa_algorithm_t alg )
3517{
3518 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3519 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3520 psa_hmac_internal_data hmac;
3521 psa_status_t status, cleanup_status;
3522
Hanno Becker3b339e22018-11-13 20:56:14 +00003523 unsigned char *Ai;
3524 size_t Ai_len;
3525
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003526 /* We can't be wanting more output after block 0xff, otherwise
3527 * the capacity check in psa_generator_read() would have
3528 * prevented this call. It could happen only if the generator
3529 * object was corrupted or if this function is called directly
3530 * inside the library. */
3531 if( tls12_prf->block_number == 0xff )
3532 return( PSA_ERROR_BAD_STATE );
3533
3534 /* We need a new block */
3535 ++tls12_prf->block_number;
3536 tls12_prf->offset_in_block = 0;
3537
3538 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
3539 *
3540 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
3541 *
3542 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
3543 * HMAC_hash(secret, A(2) + seed) +
3544 * HMAC_hash(secret, A(3) + seed) + ...
3545 *
3546 * A(0) = seed
3547 * A(i) = HMAC_hash( secret, A(i-1) )
3548 *
3549 * The `psa_tls12_prf_generator` structures saves the block
3550 * `HMAC_hash(secret, A(i) + seed)` from which the output
3551 * is currently extracted as `output_block`, while
3552 * `A(i) + seed` is stored in `Ai_with_seed`.
3553 *
3554 * Generating a new block means recalculating `Ai_with_seed`
3555 * from the A(i)-part of it, and afterwards recalculating
3556 * `output_block`.
3557 *
3558 * A(0) is computed at setup time.
3559 *
3560 */
3561
3562 psa_hmac_init_internal( &hmac );
3563
3564 /* We must distinguish the calculation of A(1) from those
3565 * of A(2) and higher, because A(0)=seed has a different
3566 * length than the other A(i). */
3567 if( tls12_prf->block_number == 1 )
3568 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003569 Ai = tls12_prf->Ai_with_seed + hash_length;
3570 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003571 }
3572 else
3573 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003574 Ai = tls12_prf->Ai_with_seed;
3575 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003576 }
3577
Hanno Becker3b339e22018-11-13 20:56:14 +00003578 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
3579 status = psa_hmac_setup_internal( &hmac,
3580 tls12_prf->key,
3581 tls12_prf->key_len,
3582 hash_alg );
3583 if( status != PSA_SUCCESS )
3584 goto cleanup;
3585
3586 status = psa_hash_update( &hmac.hash_ctx,
3587 Ai, Ai_len );
3588 if( status != PSA_SUCCESS )
3589 goto cleanup;
3590
3591 status = psa_hmac_finish_internal( &hmac,
3592 tls12_prf->Ai_with_seed,
3593 hash_length );
3594 if( status != PSA_SUCCESS )
3595 goto cleanup;
3596
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003597 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
3598 status = psa_hmac_setup_internal( &hmac,
3599 tls12_prf->key,
3600 tls12_prf->key_len,
3601 hash_alg );
3602 if( status != PSA_SUCCESS )
3603 goto cleanup;
3604
3605 status = psa_hash_update( &hmac.hash_ctx,
3606 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003607 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003608 if( status != PSA_SUCCESS )
3609 goto cleanup;
3610
3611 status = psa_hmac_finish_internal( &hmac,
3612 tls12_prf->output_block,
3613 hash_length );
3614 if( status != PSA_SUCCESS )
3615 goto cleanup;
3616
3617cleanup:
3618
3619 cleanup_status = psa_hmac_abort_internal( &hmac );
3620 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
3621 status = cleanup_status;
3622
3623 return( status );
3624}
3625
3626/* Read some bytes from an TLS-1.2-PRF-based generator.
3627 * See Section 5 of RFC 5246. */
3628static psa_status_t psa_generator_tls12_prf_read(
3629 psa_tls12_prf_generator_t *tls12_prf,
3630 psa_algorithm_t alg,
3631 uint8_t *output,
3632 size_t output_length )
3633{
3634 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3635 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3636 psa_status_t status;
3637
3638 while( output_length != 0 )
3639 {
3640 /* Copy what remains of the current block */
3641 uint8_t n = hash_length - tls12_prf->offset_in_block;
3642
3643 /* Check if we have fully processed the current block. */
3644 if( n == 0 )
3645 {
3646 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
3647 alg );
3648 if( status != PSA_SUCCESS )
3649 return( status );
3650
3651 continue;
3652 }
3653
3654 if( n > output_length )
3655 n = (uint8_t) output_length;
3656 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
3657 n );
3658 output += n;
3659 output_length -= n;
3660 tls12_prf->offset_in_block += n;
3661 }
3662
3663 return( PSA_SUCCESS );
3664}
Gilles Peskinebef7f142018-07-12 17:22:21 +02003665#endif /* MBEDTLS_MD_C */
3666
Gilles Peskineeab56e42018-07-12 17:12:33 +02003667psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3668 uint8_t *output,
3669 size_t output_length )
3670{
3671 psa_status_t status;
3672
3673 if( output_length > generator->capacity )
3674 {
3675 generator->capacity = 0;
3676 /* Go through the error path to wipe all confidential data now
3677 * that the generator object is useless. */
3678 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3679 goto exit;
3680 }
3681 if( output_length == 0 &&
3682 generator->capacity == 0 && generator->alg == 0 )
3683 {
3684 /* Edge case: this is a blank or finished generator, and 0
3685 * bytes were requested. The right error in this case could
3686 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3687 * INSUFFICIENT_CAPACITY, which is right for a finished
3688 * generator, for consistency with the case when
3689 * output_length > 0. */
3690 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3691 }
3692 generator->capacity -= output_length;
3693
Gilles Peskine751d9652018-09-18 12:05:44 +02003694 if( generator->alg == PSA_ALG_SELECT_RAW )
3695 {
Gilles Peskine211a4362018-10-25 22:22:31 +02003696 /* Initially, the capacity of a selection generator is always
3697 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
3698 * abbreviated in this comment as `size`. When the remaining
3699 * capacity is `c`, the next bytes to serve start `c` bytes
3700 * from the end of the buffer, i.e. `size - c` from the
3701 * beginning of the buffer. Since `generator->capacity` was just
3702 * decremented above, we need to serve the bytes from
3703 * `size - generator->capacity - output_length` to
3704 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02003705 size_t offset =
3706 generator->ctx.buffer.size - generator->capacity - output_length;
3707 memcpy( output, generator->ctx.buffer.data + offset, output_length );
3708 status = PSA_SUCCESS;
3709 }
3710 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003711#if defined(MBEDTLS_MD_C)
3712 if( PSA_ALG_IS_HKDF( generator->alg ) )
3713 {
3714 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3715 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3716 output, output_length );
3717 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003718 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3719 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003720 {
3721 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
3722 generator->alg, output,
3723 output_length );
3724 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003725 else
3726#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003727 {
3728 return( PSA_ERROR_BAD_STATE );
3729 }
3730
3731exit:
3732 if( status != PSA_SUCCESS )
3733 {
3734 psa_generator_abort( generator );
3735 memset( output, '!', output_length );
3736 }
3737 return( status );
3738}
3739
Gilles Peskine08542d82018-07-19 17:05:42 +02003740#if defined(MBEDTLS_DES_C)
3741static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3742{
3743 if( data_size >= 8 )
3744 mbedtls_des_key_set_parity( data );
3745 if( data_size >= 16 )
3746 mbedtls_des_key_set_parity( data + 8 );
3747 if( data_size >= 24 )
3748 mbedtls_des_key_set_parity( data + 16 );
3749}
3750#endif /* MBEDTLS_DES_C */
3751
Gilles Peskinec5487a82018-12-03 18:08:14 +01003752psa_status_t psa_generator_import_key( psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003753 psa_key_type_t type,
3754 size_t bits,
3755 psa_crypto_generator_t *generator )
3756{
3757 uint8_t *data = NULL;
3758 size_t bytes = PSA_BITS_TO_BYTES( bits );
3759 psa_status_t status;
3760
3761 if( ! key_type_is_raw_bytes( type ) )
3762 return( PSA_ERROR_INVALID_ARGUMENT );
3763 if( bits % 8 != 0 )
3764 return( PSA_ERROR_INVALID_ARGUMENT );
3765 data = mbedtls_calloc( 1, bytes );
3766 if( data == NULL )
3767 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3768
3769 status = psa_generator_read( generator, data, bytes );
3770 if( status != PSA_SUCCESS )
3771 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003772#if defined(MBEDTLS_DES_C)
3773 if( type == PSA_KEY_TYPE_DES )
3774 psa_des_set_key_parity( data, bytes );
3775#endif /* MBEDTLS_DES_C */
Gilles Peskinec5487a82018-12-03 18:08:14 +01003776 status = psa_import_key( handle, type, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003777
3778exit:
3779 mbedtls_free( data );
3780 return( status );
3781}
3782
3783
3784
3785/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003786/* Key derivation */
3787/****************************************************************/
3788
Gilles Peskinea05219c2018-11-16 16:02:56 +01003789#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02003790/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01003791 * of the HKDF algorithm.
3792 *
3793 * Note that if this function fails, you must call psa_generator_abort()
3794 * to potentially free embedded data structures and wipe confidential data.
3795 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003796static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003797 const uint8_t *secret,
3798 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02003799 psa_algorithm_t hash_alg,
3800 const uint8_t *salt,
3801 size_t salt_length,
3802 const uint8_t *label,
3803 size_t label_length )
3804{
3805 psa_status_t status;
3806 status = psa_hmac_setup_internal( &hkdf->hmac,
3807 salt, salt_length,
Gilles Peskine00709fa2018-08-22 18:25:41 +02003808 PSA_ALG_HMAC_GET_HASH( hash_alg ) );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003809 if( status != PSA_SUCCESS )
3810 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003811 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003812 if( status != PSA_SUCCESS )
3813 return( status );
3814 status = psa_hmac_finish_internal( &hkdf->hmac,
3815 hkdf->prk,
3816 sizeof( hkdf->prk ) );
3817 if( status != PSA_SUCCESS )
3818 return( status );
3819 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
3820 hkdf->block_number = 0;
3821 hkdf->info_length = label_length;
3822 if( label_length != 0 )
3823 {
3824 hkdf->info = mbedtls_calloc( 1, label_length );
3825 if( hkdf->info == NULL )
3826 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3827 memcpy( hkdf->info, label, label_length );
3828 }
3829 return( PSA_SUCCESS );
3830}
Gilles Peskinea05219c2018-11-16 16:02:56 +01003831#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003832
Gilles Peskinea05219c2018-11-16 16:02:56 +01003833#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01003834/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
3835 *
3836 * Note that if this function fails, you must call psa_generator_abort()
3837 * to potentially free embedded data structures and wipe confidential data.
3838 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003839static psa_status_t psa_generator_tls12_prf_setup(
3840 psa_tls12_prf_generator_t *tls12_prf,
3841 const unsigned char *key,
3842 size_t key_len,
3843 psa_algorithm_t hash_alg,
3844 const uint8_t *salt,
3845 size_t salt_length,
3846 const uint8_t *label,
3847 size_t label_length )
3848{
3849 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00003850 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
3851 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003852
3853 tls12_prf->key = mbedtls_calloc( 1, key_len );
3854 if( tls12_prf->key == NULL )
3855 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3856 tls12_prf->key_len = key_len;
3857 memcpy( tls12_prf->key, key, key_len );
3858
Hanno Becker580fba12018-11-13 20:50:45 +00003859 overflow = ( salt_length + label_length < salt_length ) ||
3860 ( salt_length + label_length + hash_length < hash_length );
3861 if( overflow )
3862 return( PSA_ERROR_INVALID_ARGUMENT );
3863
3864 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
3865 if( tls12_prf->Ai_with_seed == NULL )
3866 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3867 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
3868
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003869 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
3870 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00003871 if( label_length != 0 )
3872 {
3873 memcpy( tls12_prf->Ai_with_seed + hash_length,
3874 label, label_length );
3875 }
3876
3877 if( salt_length != 0 )
3878 {
3879 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
3880 salt, salt_length );
3881 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003882
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003883 /* The first block gets generated when
3884 * psa_generator_read() is called. */
3885 tls12_prf->block_number = 0;
3886 tls12_prf->offset_in_block = hash_length;
3887
3888 return( PSA_SUCCESS );
3889}
Hanno Becker1aaedc02018-11-16 11:35:34 +00003890
3891/* Set up a TLS-1.2-PSK-to-MS-based generator. */
3892static psa_status_t psa_generator_tls12_psk_to_ms_setup(
3893 psa_tls12_prf_generator_t *tls12_prf,
3894 const unsigned char *psk,
3895 size_t psk_len,
3896 psa_algorithm_t hash_alg,
3897 const uint8_t *salt,
3898 size_t salt_length,
3899 const uint8_t *label,
3900 size_t label_length )
3901{
3902 psa_status_t status;
3903 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
3904
3905 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
3906 return( PSA_ERROR_INVALID_ARGUMENT );
3907
3908 /* Quoting RFC 4279, Section 2:
3909 *
3910 * The premaster secret is formed as follows: if the PSK is N octets
3911 * long, concatenate a uint16 with the value N, N zero octets, a second
3912 * uint16 with the value N, and the PSK itself.
3913 */
3914
3915 pms[0] = ( psk_len >> 8 ) & 0xff;
3916 pms[1] = ( psk_len >> 0 ) & 0xff;
3917 memset( pms + 2, 0, psk_len );
3918 pms[2 + psk_len + 0] = pms[0];
3919 pms[2 + psk_len + 1] = pms[1];
3920 memcpy( pms + 4 + psk_len, psk, psk_len );
3921
3922 status = psa_generator_tls12_prf_setup( tls12_prf,
3923 pms, 4 + 2 * psk_len,
3924 hash_alg,
3925 salt, salt_length,
3926 label, label_length );
3927
Gilles Peskine3f108122018-12-07 18:14:53 +01003928 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00003929 return( status );
3930}
Gilles Peskinea05219c2018-11-16 16:02:56 +01003931#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003932
Gilles Peskine346797d2018-11-16 16:05:06 +01003933/* Note that if this function fails, you must call psa_generator_abort()
3934 * to potentially free embedded data structures and wipe confidential data.
3935 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003936static psa_status_t psa_key_derivation_internal(
3937 psa_crypto_generator_t *generator,
3938 const uint8_t *secret, size_t secret_length,
3939 psa_algorithm_t alg,
3940 const uint8_t *salt, size_t salt_length,
3941 const uint8_t *label, size_t label_length,
3942 size_t capacity )
3943{
3944 psa_status_t status;
3945 size_t max_capacity;
3946
3947 /* Set generator->alg even on failure so that abort knows what to do. */
3948 generator->alg = alg;
3949
Gilles Peskine751d9652018-09-18 12:05:44 +02003950 if( alg == PSA_ALG_SELECT_RAW )
3951 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003952 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02003953 if( salt_length != 0 )
3954 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01003955 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02003956 if( label_length != 0 )
3957 return( PSA_ERROR_INVALID_ARGUMENT );
3958 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
3959 if( generator->ctx.buffer.data == NULL )
3960 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3961 memcpy( generator->ctx.buffer.data, secret, secret_length );
3962 generator->ctx.buffer.size = secret_length;
3963 max_capacity = secret_length;
3964 status = PSA_SUCCESS;
3965 }
3966 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003967#if defined(MBEDTLS_MD_C)
3968 if( PSA_ALG_IS_HKDF( alg ) )
3969 {
3970 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3971 size_t hash_size = PSA_HASH_SIZE( hash_alg );
3972 if( hash_size == 0 )
3973 return( PSA_ERROR_NOT_SUPPORTED );
3974 max_capacity = 255 * hash_size;
3975 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
3976 secret, secret_length,
3977 hash_alg,
3978 salt, salt_length,
3979 label, label_length );
3980 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003981 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
3982 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
3983 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003984 {
3985 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3986 size_t hash_size = PSA_HASH_SIZE( hash_alg );
3987
3988 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
3989 if( hash_alg != PSA_ALG_SHA_256 &&
3990 hash_alg != PSA_ALG_SHA_384 )
3991 {
3992 return( PSA_ERROR_NOT_SUPPORTED );
3993 }
3994
3995 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00003996
3997 if( PSA_ALG_IS_TLS12_PRF( alg ) )
3998 {
3999 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
4000 secret, secret_length,
4001 hash_alg, salt, salt_length,
4002 label, label_length );
4003 }
4004 else
4005 {
4006 status = psa_generator_tls12_psk_to_ms_setup(
4007 &generator->ctx.tls12_prf,
4008 secret, secret_length,
4009 hash_alg, salt, salt_length,
4010 label, label_length );
4011 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004012 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004013 else
4014#endif
4015 {
4016 return( PSA_ERROR_NOT_SUPPORTED );
4017 }
4018
4019 if( status != PSA_SUCCESS )
4020 return( status );
4021
4022 if( capacity <= max_capacity )
4023 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02004024 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
4025 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004026 else
4027 return( PSA_ERROR_INVALID_ARGUMENT );
4028
4029 return( PSA_SUCCESS );
4030}
4031
Gilles Peskineea0fb492018-07-12 17:17:20 +02004032psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004033 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004034 psa_algorithm_t alg,
4035 const uint8_t *salt,
4036 size_t salt_length,
4037 const uint8_t *label,
4038 size_t label_length,
4039 size_t capacity )
4040{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004041 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004042 psa_status_t status;
4043
4044 if( generator->alg != 0 )
4045 return( PSA_ERROR_BAD_STATE );
4046
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004047 /* Make sure that alg is a key derivation algorithm. This prevents
4048 * key selection algorithms, which psa_key_derivation_internal
4049 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004050 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4051 return( PSA_ERROR_INVALID_ARGUMENT );
4052
Gilles Peskinec5487a82018-12-03 18:08:14 +01004053 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004054 if( status != PSA_SUCCESS )
4055 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004056
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004057 if( slot->type != PSA_KEY_TYPE_DERIVE )
4058 return( PSA_ERROR_INVALID_ARGUMENT );
4059
4060 status = psa_key_derivation_internal( generator,
4061 slot->data.raw.data,
4062 slot->data.raw.bytes,
4063 alg,
4064 salt, salt_length,
4065 label, label_length,
4066 capacity );
4067 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004068 psa_generator_abort( generator );
4069 return( status );
4070}
4071
4072
4073
4074/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004075/* Key agreement */
4076/****************************************************************/
4077
Gilles Peskinea05219c2018-11-16 16:02:56 +01004078#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004079static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4080 size_t peer_key_length,
4081 const mbedtls_ecp_keypair *our_key,
4082 uint8_t *shared_secret,
4083 size_t shared_secret_size,
4084 size_t *shared_secret_length )
4085{
4086 mbedtls_pk_context pk;
4087 mbedtls_ecp_keypair *their_key = NULL;
4088 mbedtls_ecdh_context ecdh;
4089 int ret;
4090 mbedtls_ecdh_init( &ecdh );
4091 mbedtls_pk_init( &pk );
4092
4093 ret = mbedtls_pk_parse_public_key( &pk, peer_key, peer_key_length );
4094 if( ret != 0 )
4095 goto exit;
Gilles Peskine88714d72018-10-25 23:07:25 +02004096 switch( mbedtls_pk_get_type( &pk ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004097 {
Gilles Peskine88714d72018-10-25 23:07:25 +02004098 case MBEDTLS_PK_ECKEY:
4099 case MBEDTLS_PK_ECKEY_DH:
4100 break;
4101 default:
4102 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
4103 goto exit;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004104 }
4105 their_key = mbedtls_pk_ec( pk );
Gilles Peskineb4086612018-11-14 20:51:23 +01004106 if( their_key->grp.id != our_key->grp.id )
4107 {
4108 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
4109 goto exit;
4110 }
4111
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004112 ret = mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS );
4113 if( ret != 0 )
4114 goto exit;
4115 ret = mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS );
4116 if( ret != 0 )
4117 goto exit;
4118
4119 ret = mbedtls_ecdh_calc_secret( &ecdh,
4120 shared_secret_length,
4121 shared_secret, shared_secret_size,
4122 mbedtls_ctr_drbg_random,
4123 &global_data.ctr_drbg );
4124
4125exit:
4126 mbedtls_pk_free( &pk );
4127 mbedtls_ecdh_free( &ecdh );
4128 return( mbedtls_to_psa_error( ret ) );
4129}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004130#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004131
Gilles Peskine01d718c2018-09-18 12:01:02 +02004132#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4133
Gilles Peskine346797d2018-11-16 16:05:06 +01004134/* Note that if this function fails, you must call psa_generator_abort()
4135 * to potentially free embedded data structures and wipe confidential data.
4136 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004137static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
Gilles Peskine2f060a82018-12-04 17:12:32 +01004138 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004139 const uint8_t *peer_key,
4140 size_t peer_key_length,
4141 psa_algorithm_t alg )
4142{
4143 psa_status_t status;
4144 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4145 size_t shared_secret_length = 0;
4146
4147 /* Step 1: run the secret agreement algorithm to generate the shared
4148 * secret. */
4149 switch( PSA_ALG_KEY_AGREEMENT_GET_BASE( alg ) )
4150 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004151#if defined(MBEDTLS_ECDH_C)
4152 case PSA_ALG_ECDH_BASE:
4153 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
4154 return( PSA_ERROR_INVALID_ARGUMENT );
4155 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
4156 private_key->data.ecp,
4157 shared_secret,
4158 sizeof( shared_secret ),
4159 &shared_secret_length );
4160 break;
4161#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004162 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01004163 (void) private_key;
4164 (void) peer_key;
4165 (void) peer_key_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004166 return( PSA_ERROR_NOT_SUPPORTED );
4167 }
4168 if( status != PSA_SUCCESS )
4169 goto exit;
4170
4171 /* Step 2: set up the key derivation to generate key material from
4172 * the shared secret. */
4173 status = psa_key_derivation_internal( generator,
4174 shared_secret, shared_secret_length,
4175 PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ),
4176 NULL, 0, NULL, 0,
4177 PSA_GENERATOR_UNBRIDLED_CAPACITY );
4178exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01004179 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004180 return( status );
4181}
4182
4183psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004184 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004185 const uint8_t *peer_key,
4186 size_t peer_key_length,
4187 psa_algorithm_t alg )
4188{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004189 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004190 psa_status_t status;
4191 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
4192 return( PSA_ERROR_INVALID_ARGUMENT );
4193 status = psa_get_key_from_slot( private_key, &slot,
4194 PSA_KEY_USAGE_DERIVE, alg );
4195 if( status != PSA_SUCCESS )
4196 return( status );
Gilles Peskine346797d2018-11-16 16:05:06 +01004197 status = psa_key_agreement_internal( generator,
4198 slot,
4199 peer_key, peer_key_length,
4200 alg );
4201 if( status != PSA_SUCCESS )
4202 psa_generator_abort( generator );
4203 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004204}
4205
4206
4207
4208/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004209/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02004210/****************************************************************/
4211
4212psa_status_t psa_generate_random( uint8_t *output,
4213 size_t output_size )
4214{
itayzafrir0adf0fc2018-09-06 16:24:41 +03004215 int ret;
4216 GUARD_MODULE_INITIALIZED;
4217
4218 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02004219 return( mbedtls_to_psa_error( ret ) );
4220}
4221
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004222#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
avolinski13beb102018-11-20 16:51:49 +02004223
4224/* Support function for error conversion between psa_its error codes to psa crypto */
4225static psa_status_t its_to_psa_error( psa_its_status_t ret )
4226{
4227 switch( ret )
4228 {
4229 case PSA_ITS_SUCCESS:
4230 return( PSA_SUCCESS );
4231
4232 case PSA_ITS_ERROR_KEY_NOT_FOUND:
4233 return( PSA_ERROR_EMPTY_SLOT );
4234
4235 case PSA_ITS_ERROR_STORAGE_FAILURE:
4236 return( PSA_ERROR_STORAGE_FAILURE );
4237
4238 case PSA_ITS_ERROR_INSUFFICIENT_SPACE:
4239 return( PSA_ERROR_INSUFFICIENT_STORAGE );
4240
4241 case PSA_ITS_ERROR_INVALID_KEY:
4242 case PSA_PS_ERROR_OFFSET_INVALID:
4243 case PSA_ITS_ERROR_INCORRECT_SIZE:
4244 case PSA_ITS_ERROR_BAD_POINTER:
4245 return( PSA_ERROR_INVALID_ARGUMENT );
4246
4247 case PSA_ITS_ERROR_FLAGS_NOT_SUPPORTED:
4248 return( PSA_ERROR_NOT_SUPPORTED );
4249
4250 case PSA_ITS_ERROR_WRITE_ONCE:
4251 return( PSA_ERROR_OCCUPIED_SLOT );
4252
4253 default:
4254 return( PSA_ERROR_UNKNOWN_ERROR );
4255 }
4256}
4257
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004258psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
4259 size_t seed_size )
4260{
4261 psa_status_t status;
avolinski13beb102018-11-20 16:51:49 +02004262 psa_its_status_t its_status;
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004263 struct psa_its_info_t p_info;
4264 if( global_data.initialized )
4265 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02004266
4267 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
4268 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
4269 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
4270 return( PSA_ERROR_INVALID_ARGUMENT );
4271
avolinski0d2c2662018-11-21 17:31:07 +02004272 its_status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
avolinski13beb102018-11-20 16:51:49 +02004273 status = its_to_psa_error( its_status );
4274
4275 if( PSA_ITS_ERROR_KEY_NOT_FOUND == its_status ) /* No seed exists */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004276 {
avolinski0d2c2662018-11-21 17:31:07 +02004277 its_status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 );
avolinski13beb102018-11-20 16:51:49 +02004278 status = its_to_psa_error( its_status );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004279 }
avolinski13beb102018-11-20 16:51:49 +02004280 else if( PSA_ITS_SUCCESS == its_status )
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004281 {
4282 /* You should not be here. Seed needs to be injected only once */
4283 status = PSA_ERROR_NOT_PERMITTED;
4284 }
4285 return( status );
4286}
4287#endif
4288
Gilles Peskinec5487a82018-12-03 18:08:14 +01004289psa_status_t psa_generate_key( psa_key_handle_t handle,
Gilles Peskine05d69892018-06-19 22:00:52 +02004290 psa_key_type_t type,
4291 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02004292 const void *extra,
4293 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02004294{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004295 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004296 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004297
Gilles Peskine53d991e2018-07-12 01:14:59 +02004298 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004299 return( PSA_ERROR_INVALID_ARGUMENT );
4300
Gilles Peskinec5487a82018-12-03 18:08:14 +01004301 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004302 if( status != PSA_SUCCESS )
4303 return( status );
4304
Gilles Peskine48c0ea12018-06-21 14:15:31 +02004305 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004306 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004307 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004308 if( status != PSA_SUCCESS )
4309 return( status );
4310 status = psa_generate_random( slot->data.raw.data,
4311 slot->data.raw.bytes );
4312 if( status != PSA_SUCCESS )
4313 {
4314 mbedtls_free( slot->data.raw.data );
4315 return( status );
4316 }
4317#if defined(MBEDTLS_DES_C)
4318 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004319 psa_des_set_key_parity( slot->data.raw.data,
4320 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004321#endif /* MBEDTLS_DES_C */
4322 }
4323 else
4324
4325#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
4326 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
4327 {
4328 mbedtls_rsa_context *rsa;
4329 int ret;
4330 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02004331 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
4332 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01004333 /* Accept only byte-aligned keys, for the same reasons as
4334 * in psa_import_rsa_key(). */
4335 if( bits % 8 != 0 )
4336 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02004337 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004338 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02004339 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004340 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004341 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02004342#if INT_MAX < 0xffffffff
4343 /* Check that the uint32_t value passed by the caller fits
4344 * in the range supported by this implementation. */
4345 if( p->e > INT_MAX )
4346 return( PSA_ERROR_NOT_SUPPORTED );
4347#endif
4348 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004349 }
4350 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
4351 if( rsa == NULL )
4352 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4353 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
4354 ret = mbedtls_rsa_gen_key( rsa,
4355 mbedtls_ctr_drbg_random,
4356 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004357 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02004358 exponent );
4359 if( ret != 0 )
4360 {
4361 mbedtls_rsa_free( rsa );
4362 mbedtls_free( rsa );
4363 return( mbedtls_to_psa_error( ret ) );
4364 }
4365 slot->data.rsa = rsa;
4366 }
4367 else
4368#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
4369
4370#if defined(MBEDTLS_ECP_C)
4371 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
4372 {
4373 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
4374 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
4375 const mbedtls_ecp_curve_info *curve_info =
4376 mbedtls_ecp_curve_info_from_grp_id( grp_id );
4377 mbedtls_ecp_keypair *ecp;
4378 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004379 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004380 return( PSA_ERROR_NOT_SUPPORTED );
4381 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
4382 return( PSA_ERROR_NOT_SUPPORTED );
4383 if( curve_info->bit_size != bits )
4384 return( PSA_ERROR_INVALID_ARGUMENT );
4385 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
4386 if( ecp == NULL )
4387 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4388 mbedtls_ecp_keypair_init( ecp );
4389 ret = mbedtls_ecp_gen_key( grp_id, ecp,
4390 mbedtls_ctr_drbg_random,
4391 &global_data.ctr_drbg );
4392 if( ret != 0 )
4393 {
4394 mbedtls_ecp_keypair_free( ecp );
4395 mbedtls_free( ecp );
4396 return( mbedtls_to_psa_error( ret ) );
4397 }
4398 slot->data.ecp = ecp;
4399 }
4400 else
4401#endif /* MBEDTLS_ECP_C */
4402
4403 return( PSA_ERROR_NOT_SUPPORTED );
4404
4405 slot->type = type;
Darryl Green0c6575a2018-11-07 16:05:30 +00004406
4407#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
4408 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
4409 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01004410 return( psa_save_generated_persistent_key( slot, bits ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004411 }
4412#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
4413
4414 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02004415}
4416
4417
4418/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01004419/* Module setup */
4420/****************************************************************/
4421
Gilles Peskine5e769522018-11-20 21:59:56 +01004422psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
4423 void (* entropy_init )( mbedtls_entropy_context *ctx ),
4424 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
4425{
4426 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4427 return( PSA_ERROR_BAD_STATE );
4428 global_data.entropy_init = entropy_init;
4429 global_data.entropy_free = entropy_free;
4430 return( PSA_SUCCESS );
4431}
4432
Gilles Peskinee59236f2018-01-27 23:32:46 +01004433void mbedtls_psa_crypto_free( void )
4434{
Gilles Peskine66fb1262018-12-10 16:29:04 +01004435 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004436 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4437 {
4438 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01004439 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004440 }
4441 /* Wipe all remaining data, including configuration.
4442 * In particular, this sets all state indicator to the value
4443 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01004444 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004445}
4446
4447psa_status_t psa_crypto_init( void )
4448{
Gilles Peskine66fb1262018-12-10 16:29:04 +01004449 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004450 const unsigned char drbg_seed[] = "PSA";
4451
Gilles Peskinec6b69072018-11-20 21:42:52 +01004452 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004453 if( global_data.initialized != 0 )
4454 return( PSA_SUCCESS );
4455
Gilles Peskine5e769522018-11-20 21:59:56 +01004456 /* Set default configuration if
4457 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
4458 if( global_data.entropy_init == NULL )
4459 global_data.entropy_init = mbedtls_entropy_init;
4460 if( global_data.entropy_free == NULL )
4461 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004462
Gilles Peskinec6b69072018-11-20 21:42:52 +01004463 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01004464 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004465 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004466 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01004467 status = mbedtls_to_psa_error(
4468 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
4469 mbedtls_entropy_func,
4470 &global_data.entropy,
4471 drbg_seed, sizeof( drbg_seed ) - 1 ) );
4472 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004473 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004474 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004475
Gilles Peskine66fb1262018-12-10 16:29:04 +01004476 status = psa_initialize_key_slots( );
4477 if( status != PSA_SUCCESS )
4478 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004479
4480 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01004481 global_data.initialized = 1;
4482
Gilles Peskinee59236f2018-01-27 23:32:46 +01004483exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01004484 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004485 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01004486 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004487}
4488
4489#endif /* MBEDTLS_PSA_CRYPTO_C */