blob: 74c3cfc0b2a7e57d4678d8f98e3d21d3c832abbd [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
Darryl Greend49a4992018-06-18 17:27:26 +010046/* Include internal declarations that are useful for implementing persistently
47 * stored keys. */
48#include "psa_crypto_storage.h"
49
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010050#include <stdlib.h>
51#include <string.h>
52#if defined(MBEDTLS_PLATFORM_C)
53#include "mbedtls/platform.h"
54#else
55#define mbedtls_calloc calloc
56#define mbedtls_free free
57#endif
58
Gilles Peskinea5905292018-02-07 20:59:33 +010059#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020060#include "mbedtls/asn1.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020061#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010062#include "mbedtls/blowfish.h"
63#include "mbedtls/camellia.h"
64#include "mbedtls/cipher.h"
65#include "mbedtls/ccm.h"
66#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010067#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010068#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020069#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010070#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010071#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010072#include "mbedtls/error.h"
73#include "mbedtls/gcm.h"
74#include "mbedtls/md2.h"
75#include "mbedtls/md4.h"
76#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010077#include "mbedtls/md.h"
78#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010079#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010080#include "mbedtls/pk_internal.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010081#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010082#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010083#include "mbedtls/sha1.h"
84#include "mbedtls/sha256.h"
85#include "mbedtls/sha512.h"
86#include "mbedtls/xtea.h"
87
Gilles Peskinee59236f2018-01-27 23:32:46 +010088
89
Gilles Peskine996deb12018-08-01 15:45:45 +020090#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
91
Gilles Peskinee59236f2018-01-27 23:32:46 +010092/* Implementation that should never be optimized out by the compiler */
93static void mbedtls_zeroize( void *v, size_t n )
94{
95 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
96}
97
Gilles Peskine9ef733f2018-02-07 21:05:37 +010098/* constant-time buffer comparison */
99static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
100{
101 size_t i;
102 unsigned char diff = 0;
103
104 for( i = 0; i < n; i++ )
105 diff |= a[i] ^ b[i];
106
107 return( diff );
108}
109
110
111
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100112/****************************************************************/
113/* Global data, support functions and library management */
114/****************************************************************/
115
116/* Number of key slots (plus one because 0 is not used).
117 * The value is a compile-time constant for now, for simplicity. */
Gilles Peskine828ed142018-06-18 23:25:51 +0200118#define PSA_KEY_SLOT_COUNT 32
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100119
Gilles Peskine2d277862018-06-18 15:41:12 +0200120typedef struct
121{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100122 psa_key_type_t type;
mohammad16038cc1cee2018-03-28 01:21:33 +0300123 psa_key_policy_t policy;
mohammad1603804cd712018-03-20 22:44:08 +0200124 psa_key_lifetime_t lifetime;
Gilles Peskine2d277862018-06-18 15:41:12 +0200125 union
126 {
127 struct raw_data
128 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100129 uint8_t *data;
130 size_t bytes;
131 } raw;
Gilles Peskine969ac722018-01-28 18:16:59 +0100132#if defined(MBEDTLS_RSA_C)
133 mbedtls_rsa_context *rsa;
134#endif /* MBEDTLS_RSA_C */
135#if defined(MBEDTLS_ECP_C)
136 mbedtls_ecp_keypair *ecp;
137#endif /* MBEDTLS_ECP_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100138 } data;
139} key_slot_t;
140
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200141static int key_type_is_raw_bytes( psa_key_type_t type )
142{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200143 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200144}
145
Gilles Peskine2d277862018-06-18 15:41:12 +0200146typedef struct
147{
Gilles Peskinee59236f2018-01-27 23:32:46 +0100148 int initialized;
149 mbedtls_entropy_context entropy;
150 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskine828ed142018-06-18 23:25:51 +0200151 key_slot_t key_slots[PSA_KEY_SLOT_COUNT];
Gilles Peskinee59236f2018-01-27 23:32:46 +0100152} psa_global_data_t;
153
154static psa_global_data_t global_data;
155
itayzafrir0adf0fc2018-09-06 16:24:41 +0300156#define GUARD_MODULE_INITIALIZED \
157 if( global_data.initialized == 0 ) \
158 return( PSA_ERROR_BAD_STATE );
159
Gilles Peskinee59236f2018-01-27 23:32:46 +0100160static psa_status_t mbedtls_to_psa_error( int ret )
161{
Gilles Peskinea5905292018-02-07 20:59:33 +0100162 /* If there's both a high-level code and low-level code, dispatch on
163 * the high-level code. */
164 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100165 {
166 case 0:
167 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100168
169 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
170 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
171 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
172 return( PSA_ERROR_NOT_SUPPORTED );
173 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
174 return( PSA_ERROR_HARDWARE_FAILURE );
175
176 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
177 return( PSA_ERROR_HARDWARE_FAILURE );
178
Gilles Peskine9a944802018-06-21 09:35:35 +0200179 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
180 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
181 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
182 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
183 case MBEDTLS_ERR_ASN1_INVALID_DATA:
184 return( PSA_ERROR_INVALID_ARGUMENT );
185 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
186 return( PSA_ERROR_INSUFFICIENT_MEMORY );
187 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
188 return( PSA_ERROR_BUFFER_TOO_SMALL );
189
Gilles Peskinea5905292018-02-07 20:59:33 +0100190 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
191 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
192 return( PSA_ERROR_NOT_SUPPORTED );
193 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
194 return( PSA_ERROR_HARDWARE_FAILURE );
195
196 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
197 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
198 return( PSA_ERROR_NOT_SUPPORTED );
199 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
200 return( PSA_ERROR_HARDWARE_FAILURE );
201
202 case MBEDTLS_ERR_CCM_BAD_INPUT:
203 return( PSA_ERROR_INVALID_ARGUMENT );
204 case MBEDTLS_ERR_CCM_AUTH_FAILED:
205 return( PSA_ERROR_INVALID_SIGNATURE );
206 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
207 return( PSA_ERROR_HARDWARE_FAILURE );
208
209 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
210 return( PSA_ERROR_NOT_SUPPORTED );
211 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
212 return( PSA_ERROR_INVALID_ARGUMENT );
213 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
214 return( PSA_ERROR_INSUFFICIENT_MEMORY );
215 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
216 return( PSA_ERROR_INVALID_PADDING );
217 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
218 return( PSA_ERROR_BAD_STATE );
219 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
220 return( PSA_ERROR_INVALID_SIGNATURE );
221 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
222 return( PSA_ERROR_TAMPERING_DETECTED );
223 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
224 return( PSA_ERROR_HARDWARE_FAILURE );
225
226 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
227 return( PSA_ERROR_HARDWARE_FAILURE );
228
229 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
230 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
231 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
232 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
233 return( PSA_ERROR_NOT_SUPPORTED );
234 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
235 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
236
237 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
238 return( PSA_ERROR_NOT_SUPPORTED );
239 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
240 return( PSA_ERROR_HARDWARE_FAILURE );
241
Gilles Peskinee59236f2018-01-27 23:32:46 +0100242 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
243 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
244 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
245 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100246
247 case MBEDTLS_ERR_GCM_AUTH_FAILED:
248 return( PSA_ERROR_INVALID_SIGNATURE );
249 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200250 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100251 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
252 return( PSA_ERROR_HARDWARE_FAILURE );
253
254 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
255 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
256 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
257 return( PSA_ERROR_HARDWARE_FAILURE );
258
259 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
260 return( PSA_ERROR_NOT_SUPPORTED );
261 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
262 return( PSA_ERROR_INVALID_ARGUMENT );
263 case MBEDTLS_ERR_MD_ALLOC_FAILED:
264 return( PSA_ERROR_INSUFFICIENT_MEMORY );
265 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
266 return( PSA_ERROR_STORAGE_FAILURE );
267 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
268 return( PSA_ERROR_HARDWARE_FAILURE );
269
Gilles Peskinef76aa772018-10-29 19:24:33 +0100270 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
271 return( PSA_ERROR_STORAGE_FAILURE );
272 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
273 return( PSA_ERROR_INVALID_ARGUMENT );
274 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
275 return( PSA_ERROR_INVALID_ARGUMENT );
276 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
277 return( PSA_ERROR_BUFFER_TOO_SMALL );
278 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
279 return( PSA_ERROR_INVALID_ARGUMENT );
280 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
281 return( PSA_ERROR_INVALID_ARGUMENT );
282 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
283 return( PSA_ERROR_INVALID_ARGUMENT );
284 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
285 return( PSA_ERROR_INSUFFICIENT_MEMORY );
286
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100287 case MBEDTLS_ERR_PK_ALLOC_FAILED:
288 return( PSA_ERROR_INSUFFICIENT_MEMORY );
289 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
290 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
291 return( PSA_ERROR_INVALID_ARGUMENT );
292 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100293 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100294 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
295 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
296 return( PSA_ERROR_INVALID_ARGUMENT );
297 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
298 return( PSA_ERROR_NOT_SUPPORTED );
299 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
300 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
301 return( PSA_ERROR_NOT_PERMITTED );
302 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
303 return( PSA_ERROR_INVALID_ARGUMENT );
304 case MBEDTLS_ERR_PK_INVALID_ALG:
305 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
306 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
307 return( PSA_ERROR_NOT_SUPPORTED );
308 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
309 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100310 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
311 return( PSA_ERROR_HARDWARE_FAILURE );
312
313 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
314 return( PSA_ERROR_HARDWARE_FAILURE );
315
316 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
317 return( PSA_ERROR_INVALID_ARGUMENT );
318 case MBEDTLS_ERR_RSA_INVALID_PADDING:
319 return( PSA_ERROR_INVALID_PADDING );
320 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
321 return( PSA_ERROR_HARDWARE_FAILURE );
322 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
323 return( PSA_ERROR_INVALID_ARGUMENT );
324 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
325 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
326 return( PSA_ERROR_TAMPERING_DETECTED );
327 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
328 return( PSA_ERROR_INVALID_SIGNATURE );
329 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
330 return( PSA_ERROR_BUFFER_TOO_SMALL );
331 case MBEDTLS_ERR_RSA_RNG_FAILED:
332 return( PSA_ERROR_INSUFFICIENT_MEMORY );
333 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
334 return( PSA_ERROR_NOT_SUPPORTED );
335 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
336 return( PSA_ERROR_HARDWARE_FAILURE );
337
338 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
339 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
340 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
341 return( PSA_ERROR_HARDWARE_FAILURE );
342
343 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
344 return( PSA_ERROR_INVALID_ARGUMENT );
345 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
346 return( PSA_ERROR_HARDWARE_FAILURE );
347
itayzafrir5c753392018-05-08 11:18:38 +0300348 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300349 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300350 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300351 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
352 return( PSA_ERROR_BUFFER_TOO_SMALL );
353 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
354 return( PSA_ERROR_NOT_SUPPORTED );
355 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
356 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
357 return( PSA_ERROR_INVALID_SIGNATURE );
358 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
359 return( PSA_ERROR_INSUFFICIENT_MEMORY );
360 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
361 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300362
Gilles Peskinee59236f2018-01-27 23:32:46 +0100363 default:
364 return( PSA_ERROR_UNKNOWN_ERROR );
365 }
366}
367
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200368
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200369
370
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100371/****************************************************************/
372/* Key management */
373/****************************************************************/
374
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100375#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200376static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
377{
378 switch( grpid )
379 {
380 case MBEDTLS_ECP_DP_SECP192R1:
381 return( PSA_ECC_CURVE_SECP192R1 );
382 case MBEDTLS_ECP_DP_SECP224R1:
383 return( PSA_ECC_CURVE_SECP224R1 );
384 case MBEDTLS_ECP_DP_SECP256R1:
385 return( PSA_ECC_CURVE_SECP256R1 );
386 case MBEDTLS_ECP_DP_SECP384R1:
387 return( PSA_ECC_CURVE_SECP384R1 );
388 case MBEDTLS_ECP_DP_SECP521R1:
389 return( PSA_ECC_CURVE_SECP521R1 );
390 case MBEDTLS_ECP_DP_BP256R1:
391 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
392 case MBEDTLS_ECP_DP_BP384R1:
393 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
394 case MBEDTLS_ECP_DP_BP512R1:
395 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
396 case MBEDTLS_ECP_DP_CURVE25519:
397 return( PSA_ECC_CURVE_CURVE25519 );
398 case MBEDTLS_ECP_DP_SECP192K1:
399 return( PSA_ECC_CURVE_SECP192K1 );
400 case MBEDTLS_ECP_DP_SECP224K1:
401 return( PSA_ECC_CURVE_SECP224K1 );
402 case MBEDTLS_ECP_DP_SECP256K1:
403 return( PSA_ECC_CURVE_SECP256K1 );
404 case MBEDTLS_ECP_DP_CURVE448:
405 return( PSA_ECC_CURVE_CURVE448 );
406 default:
407 return( 0 );
408 }
409}
410
Gilles Peskine12313cd2018-06-20 00:20:32 +0200411static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
412{
413 switch( curve )
414 {
415 case PSA_ECC_CURVE_SECP192R1:
416 return( MBEDTLS_ECP_DP_SECP192R1 );
417 case PSA_ECC_CURVE_SECP224R1:
418 return( MBEDTLS_ECP_DP_SECP224R1 );
419 case PSA_ECC_CURVE_SECP256R1:
420 return( MBEDTLS_ECP_DP_SECP256R1 );
421 case PSA_ECC_CURVE_SECP384R1:
422 return( MBEDTLS_ECP_DP_SECP384R1 );
423 case PSA_ECC_CURVE_SECP521R1:
424 return( MBEDTLS_ECP_DP_SECP521R1 );
425 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
426 return( MBEDTLS_ECP_DP_BP256R1 );
427 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
428 return( MBEDTLS_ECP_DP_BP384R1 );
429 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
430 return( MBEDTLS_ECP_DP_BP512R1 );
431 case PSA_ECC_CURVE_CURVE25519:
432 return( MBEDTLS_ECP_DP_CURVE25519 );
433 case PSA_ECC_CURVE_SECP192K1:
434 return( MBEDTLS_ECP_DP_SECP192K1 );
435 case PSA_ECC_CURVE_SECP224K1:
436 return( MBEDTLS_ECP_DP_SECP224K1 );
437 case PSA_ECC_CURVE_SECP256K1:
438 return( MBEDTLS_ECP_DP_SECP256K1 );
439 case PSA_ECC_CURVE_CURVE448:
440 return( MBEDTLS_ECP_DP_CURVE448 );
441 default:
442 return( MBEDTLS_ECP_DP_NONE );
443 }
444}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100445#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200446
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200447static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
448 size_t bits,
449 struct raw_data *raw )
450{
451 /* Check that the bit size is acceptable for the key type */
452 switch( type )
453 {
454 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200455 if( bits == 0 )
456 {
457 raw->bytes = 0;
458 raw->data = NULL;
459 return( PSA_SUCCESS );
460 }
461 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200462#if defined(MBEDTLS_MD_C)
463 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200464#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200465 case PSA_KEY_TYPE_DERIVE:
466 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200467#if defined(MBEDTLS_AES_C)
468 case PSA_KEY_TYPE_AES:
469 if( bits != 128 && bits != 192 && bits != 256 )
470 return( PSA_ERROR_INVALID_ARGUMENT );
471 break;
472#endif
473#if defined(MBEDTLS_CAMELLIA_C)
474 case PSA_KEY_TYPE_CAMELLIA:
475 if( bits != 128 && bits != 192 && bits != 256 )
476 return( PSA_ERROR_INVALID_ARGUMENT );
477 break;
478#endif
479#if defined(MBEDTLS_DES_C)
480 case PSA_KEY_TYPE_DES:
481 if( bits != 64 && bits != 128 && bits != 192 )
482 return( PSA_ERROR_INVALID_ARGUMENT );
483 break;
484#endif
485#if defined(MBEDTLS_ARC4_C)
486 case PSA_KEY_TYPE_ARC4:
487 if( bits < 8 || bits > 2048 )
488 return( PSA_ERROR_INVALID_ARGUMENT );
489 break;
490#endif
491 default:
492 return( PSA_ERROR_NOT_SUPPORTED );
493 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200494 if( bits % 8 != 0 )
495 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200496
497 /* Allocate memory for the key */
498 raw->bytes = PSA_BITS_TO_BYTES( bits );
499 raw->data = mbedtls_calloc( 1, raw->bytes );
500 if( raw->data == NULL )
501 {
502 raw->bytes = 0;
503 return( PSA_ERROR_INSUFFICIENT_MEMORY );
504 }
505 return( PSA_SUCCESS );
506}
507
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200508#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100509/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
510 * that are not a multiple of 8) well. For example, there is only
511 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
512 * way to return the exact bit size of a key.
513 * To keep things simple, reject non-byte-aligned key sizes. */
514static psa_status_t psa_check_rsa_key_byte_aligned(
515 const mbedtls_rsa_context *rsa )
516{
517 mbedtls_mpi n;
518 psa_status_t status;
519 mbedtls_mpi_init( &n );
520 status = mbedtls_to_psa_error(
521 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
522 if( status == PSA_SUCCESS )
523 {
524 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
525 status = PSA_ERROR_NOT_SUPPORTED;
526 }
527 mbedtls_mpi_free( &n );
528 return( status );
529}
530
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200531static psa_status_t psa_import_rsa_key( mbedtls_pk_context *pk,
532 mbedtls_rsa_context **p_rsa )
533{
534 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_RSA )
535 return( PSA_ERROR_INVALID_ARGUMENT );
536 else
537 {
538 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *pk );
Gilles Peskineaac64a22018-11-12 18:37:42 +0100539 /* The size of an RSA key doesn't have to be a multiple of 8.
540 * Mbed TLS supports non-byte-aligned key sizes, but not well.
541 * For example, mbedtls_rsa_get_len() returns the key size in
542 * bytes, not in bits. */
543 size_t bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
Gilles Peskine86a440b2018-11-12 18:39:40 +0100544 psa_status_t status;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200545 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
546 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +0100547 status = psa_check_rsa_key_byte_aligned( rsa );
548 if( status != PSA_SUCCESS )
549 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200550 *p_rsa = rsa;
551 return( PSA_SUCCESS );
552 }
553}
554#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
555
556#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinef76aa772018-10-29 19:24:33 +0100557/* Import an elliptic curve parsed by the mbedtls pk module. */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200558static psa_status_t psa_import_ecp_key( psa_ecc_curve_t expected_curve,
559 mbedtls_pk_context *pk,
560 mbedtls_ecp_keypair **p_ecp )
561{
562 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_ECKEY )
563 return( PSA_ERROR_INVALID_ARGUMENT );
564 else
565 {
566 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( *pk );
567 psa_ecc_curve_t actual_curve = mbedtls_ecc_group_to_psa( ecp->grp.id );
568 if( actual_curve != expected_curve )
569 return( PSA_ERROR_INVALID_ARGUMENT );
570 *p_ecp = ecp;
571 return( PSA_SUCCESS );
572 }
573}
574#endif /* defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C) */
575
Gilles Peskinef76aa772018-10-29 19:24:33 +0100576#if defined(MBEDTLS_ECP_C)
577/* Import a private key given as a byte string which is the private value
578 * in big-endian order. */
579static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
580 const uint8_t *data,
581 size_t data_length,
582 mbedtls_ecp_keypair **p_ecp )
583{
584 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
585 mbedtls_ecp_keypair *ecp = NULL;
586 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
587
588 *p_ecp = NULL;
589 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
590 if( ecp == NULL )
591 return( PSA_ERROR_INSUFFICIENT_MEMORY );
592
593 /* Load the group. */
594 status = mbedtls_to_psa_error(
595 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
596 if( status != PSA_SUCCESS )
597 goto exit;
598 /* Load the secret value. */
599 status = mbedtls_to_psa_error(
600 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
601 if( status != PSA_SUCCESS )
602 goto exit;
603 /* Validate the private key. */
604 status = mbedtls_to_psa_error(
605 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
606 if( status != PSA_SUCCESS )
607 goto exit;
608 /* Calculate the public key from the private key. */
609 status = mbedtls_to_psa_error(
610 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
611 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
612 if( status != PSA_SUCCESS )
613 goto exit;
614
615 *p_ecp = ecp;
616 return( PSA_SUCCESS );
617
618exit:
619 if( ecp != NULL )
620 {
621 mbedtls_ecp_keypair_free( ecp );
622 mbedtls_free( ecp );
623 }
624 return( status );
625}
626#endif /* defined(MBEDTLS_ECP_C) */
627
Darryl Green940d72c2018-07-13 13:18:51 +0100628static psa_status_t psa_import_key_into_slot( key_slot_t *slot,
629 const uint8_t *data,
630 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100631{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200632 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100633
Darryl Green940d72c2018-07-13 13:18:51 +0100634 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100635 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100636 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100637 if( data_length > SIZE_MAX / 8 )
638 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green940d72c2018-07-13 13:18:51 +0100639 status = prepare_raw_data_slot( slot->type,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200640 PSA_BYTES_TO_BITS( data_length ),
641 &slot->data.raw );
642 if( status != PSA_SUCCESS )
643 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200644 if( data_length != 0 )
645 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100646 }
647 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100648#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100649 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100650 {
Darryl Green940d72c2018-07-13 13:18:51 +0100651 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100652 data, data_length,
653 &slot->data.ecp );
654 if( status != PSA_SUCCESS )
655 return( status );
656 }
657 else
658#endif /* MBEDTLS_ECP_C */
Gilles Peskine969ac722018-01-28 18:16:59 +0100659#if defined(MBEDTLS_PK_PARSE_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100660 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
661 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100662 {
663 int ret;
Gilles Peskine969ac722018-01-28 18:16:59 +0100664 mbedtls_pk_context pk;
665 mbedtls_pk_init( &pk );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200666
667 /* Parse the data. */
Darryl Green940d72c2018-07-13 13:18:51 +0100668 if( PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100669 ret = mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 );
670 else
671 ret = mbedtls_pk_parse_public_key( &pk, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100672 if( ret != 0 )
673 return( mbedtls_to_psa_error( ret ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200674
675 /* We have something that the pkparse module recognizes.
676 * If it has the expected type and passes any type-specific
677 * checks, store it. */
Gilles Peskine969ac722018-01-28 18:16:59 +0100678#if defined(MBEDTLS_RSA_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100679 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200680 status = psa_import_rsa_key( &pk, &slot->data.rsa );
681 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100682#endif /* MBEDTLS_RSA_C */
683#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100684 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
685 status = psa_import_ecp_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200686 &pk, &slot->data.ecp );
687 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100688#endif /* MBEDTLS_ECP_C */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200689 {
690 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinec648d692018-06-28 08:46:13 +0200691 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200692
Gilles Peskinec648d692018-06-28 08:46:13 +0200693 /* Free the content of the pk object only on error. On success,
694 * the content of the object has been stored in the slot. */
695 if( status != PSA_SUCCESS )
696 {
697 mbedtls_pk_free( &pk );
698 return( status );
Gilles Peskine969ac722018-01-28 18:16:59 +0100699 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100700 }
701 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100702#endif /* defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100703 {
704 return( PSA_ERROR_NOT_SUPPORTED );
705 }
Darryl Green940d72c2018-07-13 13:18:51 +0100706 return( PSA_SUCCESS );
707}
708
Darryl Greend49a4992018-06-18 17:27:26 +0100709#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
710static psa_status_t psa_load_persistent_key_into_slot( psa_key_slot_t key,
711 key_slot_t *p_slot )
712{
713 psa_status_t status = PSA_SUCCESS;
714 uint8_t *key_data = NULL;
715 size_t key_data_length = 0;
716
717 status = psa_load_persistent_key( key, &( p_slot )->type,
718 &( p_slot )->policy, &key_data,
719 &key_data_length );
720 if( status != PSA_SUCCESS )
721 goto exit;
722 status = psa_import_key_into_slot( p_slot,
723 key_data, key_data_length );
724exit:
725 psa_free_persistent_key_data( key_data, key_data_length );
726 return( status );
727}
728#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
729
Darryl Green06fd18d2018-07-16 11:21:11 +0100730/* Retrieve a key slot, occupied or not. */
731static psa_status_t psa_get_key_slot( psa_key_slot_t key,
732 key_slot_t **p_slot )
733{
734 GUARD_MODULE_INITIALIZED;
735
736 /* 0 is not a valid slot number under any circumstance. This
737 * implementation provides slots number 1 to N where N is the
738 * number of available slots. */
739 if( key == 0 || key > ARRAY_LENGTH( global_data.key_slots ) )
740 return( PSA_ERROR_INVALID_ARGUMENT );
741
742 *p_slot = &global_data.key_slots[key - 1];
Darryl Greend49a4992018-06-18 17:27:26 +0100743
744#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
745 if( ( *p_slot )->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
746 {
747 /* There are two circumstances this can occur: the key material has
748 * not yet been created, or the key exists in storage but has not yet
749 * been loaded into memory. */
750 if( ( *p_slot )->type == PSA_KEY_TYPE_NONE )
751 {
752 psa_status_t status = PSA_SUCCESS;
753 status = psa_load_persistent_key_into_slot( key, *p_slot );
754 if( status != PSA_ERROR_EMPTY_SLOT )
755 return( status );
756 }
757 }
758#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
759
Darryl Green06fd18d2018-07-16 11:21:11 +0100760 return( PSA_SUCCESS );
761}
762
763/* Retrieve an empty key slot (slot with no key data, but possibly
764 * with some metadata such as a policy). */
765static psa_status_t psa_get_empty_key_slot( psa_key_slot_t key,
766 key_slot_t **p_slot )
767{
768 psa_status_t status;
769 key_slot_t *slot = NULL;
770
771 *p_slot = NULL;
772
773 status = psa_get_key_slot( key, &slot );
774 if( status != PSA_SUCCESS )
775 return( status );
776
777 if( slot->type != PSA_KEY_TYPE_NONE )
778 return( PSA_ERROR_OCCUPIED_SLOT );
779
780 *p_slot = slot;
781 return( status );
782}
783
784/** Retrieve a slot which must contain a key. The key must have allow all the
785 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
786 * operations with this algorithm. */
787static psa_status_t psa_get_key_from_slot( psa_key_slot_t key,
788 key_slot_t **p_slot,
789 psa_key_usage_t usage,
790 psa_algorithm_t alg )
791{
792 psa_status_t status;
793 key_slot_t *slot = NULL;
794
795 *p_slot = NULL;
796
797 status = psa_get_key_slot( key, &slot );
798 if( status != PSA_SUCCESS )
799 return( status );
800 if( slot->type == PSA_KEY_TYPE_NONE )
801 return( PSA_ERROR_EMPTY_SLOT );
802
803 /* Enforce that usage policy for the key slot contains all the flags
804 * required by the usage parameter. There is one exception: public
805 * keys can always be exported, so we treat public key objects as
806 * if they had the export flag. */
807 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
808 usage &= ~PSA_KEY_USAGE_EXPORT;
809 if( ( slot->policy.usage & usage ) != usage )
810 return( PSA_ERROR_NOT_PERMITTED );
811 if( alg != 0 && ( alg != slot->policy.alg ) )
812 return( PSA_ERROR_NOT_PERMITTED );
813
814 *p_slot = slot;
815 return( PSA_SUCCESS );
816}
Darryl Green940d72c2018-07-13 13:18:51 +0100817
Darryl Green40225ba2018-11-15 14:48:15 +0000818static psa_status_t psa_remove_key_data_from_memory( key_slot_t *slot )
819{
820 if( slot->type == PSA_KEY_TYPE_NONE )
821 {
822 /* No key material to clean. */
823 }
824 else if( key_type_is_raw_bytes( slot->type ) )
825 {
826 mbedtls_free( slot->data.raw.data );
827 }
828 else
829#if defined(MBEDTLS_RSA_C)
830 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
831 {
832 mbedtls_rsa_free( slot->data.rsa );
833 mbedtls_free( slot->data.rsa );
834 }
835 else
836#endif /* defined(MBEDTLS_RSA_C) */
837#if defined(MBEDTLS_ECP_C)
838 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
839 {
840 mbedtls_ecp_keypair_free( slot->data.ecp );
841 mbedtls_free( slot->data.ecp );
842 }
843 else
844#endif /* defined(MBEDTLS_ECP_C) */
845 {
846 /* Shouldn't happen: the key type is not any type that we
847 * put in. */
848 return( PSA_ERROR_TAMPERING_DETECTED );
849 }
850
851 return( PSA_SUCCESS );
852}
853
Darryl Green940d72c2018-07-13 13:18:51 +0100854psa_status_t psa_import_key( psa_key_slot_t key,
855 psa_key_type_t type,
856 const uint8_t *data,
857 size_t data_length )
858{
859 key_slot_t *slot;
860 psa_status_t status;
861
862 status = psa_get_empty_key_slot( key, &slot );
863 if( status != PSA_SUCCESS )
864 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100865
866 slot->type = type;
Darryl Green940d72c2018-07-13 13:18:51 +0100867
868 status = psa_import_key_into_slot( slot, data, data_length );
869 if( status != PSA_SUCCESS )
870 {
871 slot->type = PSA_KEY_TYPE_NONE;
872 return( status );
873 }
874
Darryl Greend49a4992018-06-18 17:27:26 +0100875#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
876 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
877 {
878 /* Store in file location */
879 status = psa_save_persistent_key( key, slot->type, &slot->policy, data,
880 data_length );
881 if( status != PSA_SUCCESS )
882 {
883 (void) psa_remove_key_data_from_memory( slot );
884 slot->type = PSA_KEY_TYPE_NONE;
885 }
886 }
887#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
888
889 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100890}
891
Gilles Peskine2d277862018-06-18 15:41:12 +0200892psa_status_t psa_destroy_key( psa_key_slot_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100893{
894 key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100895 psa_status_t status = PSA_SUCCESS;
896 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100897
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200898 status = psa_get_key_slot( key, &slot );
899 if( status != PSA_SUCCESS )
900 return( status );
Darryl Greend49a4992018-06-18 17:27:26 +0100901#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
902 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
903 {
904 storage_status = psa_destroy_persistent_key( key );
905 }
906#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
907 status = psa_remove_key_data_from_memory( slot );
908 /* Zeroize the slot to wipe metadata such as policies. */
909 mbedtls_zeroize( slot, sizeof( *slot ) );
910 if( status != PSA_SUCCESS )
911 return( status );
912 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100913}
914
Gilles Peskineb870b182018-07-06 16:02:09 +0200915/* Return the size of the key in the given slot, in bits. */
916static size_t psa_get_key_bits( const key_slot_t *slot )
917{
918 if( key_type_is_raw_bytes( slot->type ) )
919 return( slot->data.raw.bytes * 8 );
920#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200921 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +0100922 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +0200923#endif /* defined(MBEDTLS_RSA_C) */
924#if defined(MBEDTLS_ECP_C)
925 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
926 return( slot->data.ecp->grp.pbits );
927#endif /* defined(MBEDTLS_ECP_C) */
928 /* Shouldn't happen except on an empty slot. */
929 return( 0 );
930}
931
Gilles Peskine2d277862018-06-18 15:41:12 +0200932psa_status_t psa_get_key_information( psa_key_slot_t key,
933 psa_key_type_t *type,
934 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100935{
936 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200937 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100938
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100939 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200940 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100941 if( bits != NULL )
942 *bits = 0;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200943 status = psa_get_key_slot( key, &slot );
944 if( status != PSA_SUCCESS )
945 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +0200946
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100947 if( slot->type == PSA_KEY_TYPE_NONE )
948 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200949 if( type != NULL )
950 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +0200951 if( bits != NULL )
952 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100953 return( PSA_SUCCESS );
954}
955
Darryl Greendd8fb772018-11-07 16:00:44 +0000956static psa_status_t psa_internal_export_key( key_slot_t *slot,
Gilles Peskine2d277862018-06-18 15:41:12 +0200957 uint8_t *data,
958 size_t data_size,
959 size_t *data_length,
960 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100961{
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100962 *data_length = 0;
963
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200964 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300965 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +0300966
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200967 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100968 {
969 if( slot->data.raw.bytes > data_size )
970 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +0100971 if( data_size != 0 )
972 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200973 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +0100974 memset( data + slot->data.raw.bytes, 0,
975 data_size - slot->data.raw.bytes );
976 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100977 *data_length = slot->data.raw.bytes;
978 return( PSA_SUCCESS );
979 }
Gilles Peskine188c71e2018-10-29 19:26:02 +0100980#if defined(MBEDTLS_ECP_C)
981 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
982 {
Darryl Greendd8fb772018-11-07 16:00:44 +0000983 psa_status_t status;
984
Gilles Peskine188c71e2018-10-29 19:26:02 +0100985 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( slot ) );
986 if( bytes > data_size )
987 return( PSA_ERROR_BUFFER_TOO_SMALL );
988 status = mbedtls_to_psa_error(
989 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
990 if( status != PSA_SUCCESS )
991 return( status );
992 memset( data + bytes, 0, data_size - bytes );
993 *data_length = bytes;
994 return( PSA_SUCCESS );
995 }
996#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100997 else
Moran Peker17e36e12018-05-02 12:55:20 +0300998 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100999#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001000 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001001 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001002 {
Moran Pekera998bc62018-04-16 18:16:20 +03001003 mbedtls_pk_context pk;
1004 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001005 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001006 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001007#if defined(MBEDTLS_RSA_C)
1008 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001009 pk.pk_info = &mbedtls_rsa_info;
1010 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001011#else
1012 return( PSA_ERROR_NOT_SUPPORTED );
1013#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001014 }
1015 else
1016 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001017#if defined(MBEDTLS_ECP_C)
1018 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001019 pk.pk_info = &mbedtls_eckey_info;
1020 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001021#else
1022 return( PSA_ERROR_NOT_SUPPORTED );
1023#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001024 }
Moran Pekerd7326592018-05-29 16:56:39 +03001025 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001026 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +03001027 else
1028 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +03001029 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001030 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001031 /* If data_size is 0 then data may be NULL and then the
1032 * call to memset would have undefined behavior. */
1033 if( data_size != 0 )
1034 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001035 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001036 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001037 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1038 * Move the data to the beginning and erase remaining data
1039 * at the original location. */
1040 if( 2 * (size_t) ret <= data_size )
1041 {
1042 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001043 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001044 }
1045 else if( (size_t) ret < data_size )
1046 {
1047 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001048 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001049 }
Moran Pekera998bc62018-04-16 18:16:20 +03001050 *data_length = ret;
1051 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001052 }
1053 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001054#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001055 {
1056 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001057 it is valid for a special-purpose implementation to omit
1058 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001059 return( PSA_ERROR_NOT_SUPPORTED );
1060 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001061 }
1062}
1063
Gilles Peskine2d277862018-06-18 15:41:12 +02001064psa_status_t psa_export_key( psa_key_slot_t key,
1065 uint8_t *data,
1066 size_t data_size,
1067 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001068{
Darryl Greendd8fb772018-11-07 16:00:44 +00001069 key_slot_t *slot;
1070 psa_status_t status;
1071
1072 /* Set the key to empty now, so that even when there are errors, we always
1073 * set data_length to a value between 0 and data_size. On error, setting
1074 * the key to empty is a good choice because an empty key representation is
1075 * unlikely to be accepted anywhere. */
1076 *data_length = 0;
1077
1078 /* Export requires the EXPORT flag. There is an exception for public keys,
1079 * which don't require any flag, but psa_get_key_from_slot takes
1080 * care of this. */
1081 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_EXPORT, 0 );
1082 if( status != PSA_SUCCESS )
1083 return( status );
1084 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001085 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001086}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001087
Gilles Peskine2d277862018-06-18 15:41:12 +02001088psa_status_t psa_export_public_key( psa_key_slot_t key,
1089 uint8_t *data,
1090 size_t data_size,
1091 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001092{
Darryl Greendd8fb772018-11-07 16:00:44 +00001093 key_slot_t *slot;
1094 psa_status_t status;
1095
1096 /* Set the key to empty now, so that even when there are errors, we always
1097 * set data_length to a value between 0 and data_size. On error, setting
1098 * the key to empty is a good choice because an empty key representation is
1099 * unlikely to be accepted anywhere. */
1100 *data_length = 0;
1101
1102 /* Exporting a public key doesn't require a usage flag. */
1103 status = psa_get_key_from_slot( key, &slot, 0, 0 );
1104 if( status != PSA_SUCCESS )
1105 return( status );
1106 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001107 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001108}
1109
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001110
1111
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001112/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001113/* Message digests */
1114/****************************************************************/
1115
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001116static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001117{
1118 switch( alg )
1119 {
1120#if defined(MBEDTLS_MD2_C)
1121 case PSA_ALG_MD2:
1122 return( &mbedtls_md2_info );
1123#endif
1124#if defined(MBEDTLS_MD4_C)
1125 case PSA_ALG_MD4:
1126 return( &mbedtls_md4_info );
1127#endif
1128#if defined(MBEDTLS_MD5_C)
1129 case PSA_ALG_MD5:
1130 return( &mbedtls_md5_info );
1131#endif
1132#if defined(MBEDTLS_RIPEMD160_C)
1133 case PSA_ALG_RIPEMD160:
1134 return( &mbedtls_ripemd160_info );
1135#endif
1136#if defined(MBEDTLS_SHA1_C)
1137 case PSA_ALG_SHA_1:
1138 return( &mbedtls_sha1_info );
1139#endif
1140#if defined(MBEDTLS_SHA256_C)
1141 case PSA_ALG_SHA_224:
1142 return( &mbedtls_sha224_info );
1143 case PSA_ALG_SHA_256:
1144 return( &mbedtls_sha256_info );
1145#endif
1146#if defined(MBEDTLS_SHA512_C)
1147 case PSA_ALG_SHA_384:
1148 return( &mbedtls_sha384_info );
1149 case PSA_ALG_SHA_512:
1150 return( &mbedtls_sha512_info );
1151#endif
1152 default:
1153 return( NULL );
1154 }
1155}
1156
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001157psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1158{
1159 switch( operation->alg )
1160 {
Gilles Peskine81736312018-06-26 15:04:31 +02001161 case 0:
1162 /* The object has (apparently) been initialized but it is not
1163 * in use. It's ok to call abort on such an object, and there's
1164 * nothing to do. */
1165 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001166#if defined(MBEDTLS_MD2_C)
1167 case PSA_ALG_MD2:
1168 mbedtls_md2_free( &operation->ctx.md2 );
1169 break;
1170#endif
1171#if defined(MBEDTLS_MD4_C)
1172 case PSA_ALG_MD4:
1173 mbedtls_md4_free( &operation->ctx.md4 );
1174 break;
1175#endif
1176#if defined(MBEDTLS_MD5_C)
1177 case PSA_ALG_MD5:
1178 mbedtls_md5_free( &operation->ctx.md5 );
1179 break;
1180#endif
1181#if defined(MBEDTLS_RIPEMD160_C)
1182 case PSA_ALG_RIPEMD160:
1183 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1184 break;
1185#endif
1186#if defined(MBEDTLS_SHA1_C)
1187 case PSA_ALG_SHA_1:
1188 mbedtls_sha1_free( &operation->ctx.sha1 );
1189 break;
1190#endif
1191#if defined(MBEDTLS_SHA256_C)
1192 case PSA_ALG_SHA_224:
1193 case PSA_ALG_SHA_256:
1194 mbedtls_sha256_free( &operation->ctx.sha256 );
1195 break;
1196#endif
1197#if defined(MBEDTLS_SHA512_C)
1198 case PSA_ALG_SHA_384:
1199 case PSA_ALG_SHA_512:
1200 mbedtls_sha512_free( &operation->ctx.sha512 );
1201 break;
1202#endif
1203 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001204 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001205 }
1206 operation->alg = 0;
1207 return( PSA_SUCCESS );
1208}
1209
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001210psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001211 psa_algorithm_t alg )
1212{
1213 int ret;
1214 operation->alg = 0;
1215 switch( alg )
1216 {
1217#if defined(MBEDTLS_MD2_C)
1218 case PSA_ALG_MD2:
1219 mbedtls_md2_init( &operation->ctx.md2 );
1220 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1221 break;
1222#endif
1223#if defined(MBEDTLS_MD4_C)
1224 case PSA_ALG_MD4:
1225 mbedtls_md4_init( &operation->ctx.md4 );
1226 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1227 break;
1228#endif
1229#if defined(MBEDTLS_MD5_C)
1230 case PSA_ALG_MD5:
1231 mbedtls_md5_init( &operation->ctx.md5 );
1232 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1233 break;
1234#endif
1235#if defined(MBEDTLS_RIPEMD160_C)
1236 case PSA_ALG_RIPEMD160:
1237 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1238 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1239 break;
1240#endif
1241#if defined(MBEDTLS_SHA1_C)
1242 case PSA_ALG_SHA_1:
1243 mbedtls_sha1_init( &operation->ctx.sha1 );
1244 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1245 break;
1246#endif
1247#if defined(MBEDTLS_SHA256_C)
1248 case PSA_ALG_SHA_224:
1249 mbedtls_sha256_init( &operation->ctx.sha256 );
1250 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1251 break;
1252 case PSA_ALG_SHA_256:
1253 mbedtls_sha256_init( &operation->ctx.sha256 );
1254 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1255 break;
1256#endif
1257#if defined(MBEDTLS_SHA512_C)
1258 case PSA_ALG_SHA_384:
1259 mbedtls_sha512_init( &operation->ctx.sha512 );
1260 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1261 break;
1262 case PSA_ALG_SHA_512:
1263 mbedtls_sha512_init( &operation->ctx.sha512 );
1264 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1265 break;
1266#endif
1267 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001268 return( PSA_ALG_IS_HASH( alg ) ?
1269 PSA_ERROR_NOT_SUPPORTED :
1270 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001271 }
1272 if( ret == 0 )
1273 operation->alg = alg;
1274 else
1275 psa_hash_abort( operation );
1276 return( mbedtls_to_psa_error( ret ) );
1277}
1278
1279psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1280 const uint8_t *input,
1281 size_t input_length )
1282{
1283 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001284
1285 /* Don't require hash implementations to behave correctly on a
1286 * zero-length input, which may have an invalid pointer. */
1287 if( input_length == 0 )
1288 return( PSA_SUCCESS );
1289
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001290 switch( operation->alg )
1291 {
1292#if defined(MBEDTLS_MD2_C)
1293 case PSA_ALG_MD2:
1294 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1295 input, input_length );
1296 break;
1297#endif
1298#if defined(MBEDTLS_MD4_C)
1299 case PSA_ALG_MD4:
1300 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1301 input, input_length );
1302 break;
1303#endif
1304#if defined(MBEDTLS_MD5_C)
1305 case PSA_ALG_MD5:
1306 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1307 input, input_length );
1308 break;
1309#endif
1310#if defined(MBEDTLS_RIPEMD160_C)
1311 case PSA_ALG_RIPEMD160:
1312 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1313 input, input_length );
1314 break;
1315#endif
1316#if defined(MBEDTLS_SHA1_C)
1317 case PSA_ALG_SHA_1:
1318 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1319 input, input_length );
1320 break;
1321#endif
1322#if defined(MBEDTLS_SHA256_C)
1323 case PSA_ALG_SHA_224:
1324 case PSA_ALG_SHA_256:
1325 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1326 input, input_length );
1327 break;
1328#endif
1329#if defined(MBEDTLS_SHA512_C)
1330 case PSA_ALG_SHA_384:
1331 case PSA_ALG_SHA_512:
1332 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1333 input, input_length );
1334 break;
1335#endif
1336 default:
1337 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1338 break;
1339 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001340
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001341 if( ret != 0 )
1342 psa_hash_abort( operation );
1343 return( mbedtls_to_psa_error( ret ) );
1344}
1345
1346psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1347 uint8_t *hash,
1348 size_t hash_size,
1349 size_t *hash_length )
1350{
itayzafrir40835d42018-08-02 13:14:17 +03001351 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001352 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001353 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001354
1355 /* Fill the output buffer with something that isn't a valid hash
1356 * (barring an attack on the hash and deliberately-crafted input),
1357 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001358 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001359 /* If hash_size is 0 then hash may be NULL and then the
1360 * call to memset would have undefined behavior. */
1361 if( hash_size != 0 )
1362 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001363
1364 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001365 {
1366 status = PSA_ERROR_BUFFER_TOO_SMALL;
1367 goto exit;
1368 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001369
1370 switch( operation->alg )
1371 {
1372#if defined(MBEDTLS_MD2_C)
1373 case PSA_ALG_MD2:
1374 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1375 break;
1376#endif
1377#if defined(MBEDTLS_MD4_C)
1378 case PSA_ALG_MD4:
1379 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1380 break;
1381#endif
1382#if defined(MBEDTLS_MD5_C)
1383 case PSA_ALG_MD5:
1384 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1385 break;
1386#endif
1387#if defined(MBEDTLS_RIPEMD160_C)
1388 case PSA_ALG_RIPEMD160:
1389 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1390 break;
1391#endif
1392#if defined(MBEDTLS_SHA1_C)
1393 case PSA_ALG_SHA_1:
1394 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1395 break;
1396#endif
1397#if defined(MBEDTLS_SHA256_C)
1398 case PSA_ALG_SHA_224:
1399 case PSA_ALG_SHA_256:
1400 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1401 break;
1402#endif
1403#if defined(MBEDTLS_SHA512_C)
1404 case PSA_ALG_SHA_384:
1405 case PSA_ALG_SHA_512:
1406 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1407 break;
1408#endif
1409 default:
1410 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1411 break;
1412 }
itayzafrir40835d42018-08-02 13:14:17 +03001413 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001414
itayzafrir40835d42018-08-02 13:14:17 +03001415exit:
1416 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001417 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001418 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001419 return( psa_hash_abort( operation ) );
1420 }
1421 else
1422 {
1423 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001424 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001425 }
1426}
1427
Gilles Peskine2d277862018-06-18 15:41:12 +02001428psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1429 const uint8_t *hash,
1430 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001431{
1432 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1433 size_t actual_hash_length;
1434 psa_status_t status = psa_hash_finish( operation,
1435 actual_hash, sizeof( actual_hash ),
1436 &actual_hash_length );
1437 if( status != PSA_SUCCESS )
1438 return( status );
1439 if( actual_hash_length != hash_length )
1440 return( PSA_ERROR_INVALID_SIGNATURE );
1441 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1442 return( PSA_ERROR_INVALID_SIGNATURE );
1443 return( PSA_SUCCESS );
1444}
1445
1446
1447
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001448/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001449/* MAC */
1450/****************************************************************/
1451
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001452static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001453 psa_algorithm_t alg,
1454 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001455 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001456 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001457{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001458 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001459 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001460
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001461 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001462 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001463
Gilles Peskine8c9def32018-02-08 10:02:12 +01001464 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1465 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001466 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001467 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001468 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001469 mode = MBEDTLS_MODE_STREAM;
1470 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001471 case PSA_ALG_CTR:
1472 mode = MBEDTLS_MODE_CTR;
1473 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001474 case PSA_ALG_CFB:
1475 mode = MBEDTLS_MODE_CFB;
1476 break;
1477 case PSA_ALG_OFB:
1478 mode = MBEDTLS_MODE_OFB;
1479 break;
1480 case PSA_ALG_CBC_NO_PADDING:
1481 mode = MBEDTLS_MODE_CBC;
1482 break;
1483 case PSA_ALG_CBC_PKCS7:
1484 mode = MBEDTLS_MODE_CBC;
1485 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001486 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001487 mode = MBEDTLS_MODE_CCM;
1488 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001489 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001490 mode = MBEDTLS_MODE_GCM;
1491 break;
1492 default:
1493 return( NULL );
1494 }
1495 }
1496 else if( alg == PSA_ALG_CMAC )
1497 mode = MBEDTLS_MODE_ECB;
1498 else if( alg == PSA_ALG_GMAC )
1499 mode = MBEDTLS_MODE_GCM;
1500 else
1501 return( NULL );
1502
1503 switch( key_type )
1504 {
1505 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001506 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001507 break;
1508 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001509 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1510 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001511 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001512 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001513 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001514 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001515 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1516 * but two-key Triple-DES is functionally three-key Triple-DES
1517 * with K1=K3, so that's how we present it to mbedtls. */
1518 if( key_bits == 128 )
1519 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001520 break;
1521 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001522 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001523 break;
1524 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001525 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001526 break;
1527 default:
1528 return( NULL );
1529 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001530 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001531 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001532
Jaeden Amero23bbb752018-06-26 14:16:54 +01001533 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1534 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001535}
1536
Gilles Peskinea05219c2018-11-16 16:02:56 +01001537#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001538static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001539{
Gilles Peskine2d277862018-06-18 15:41:12 +02001540 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001541 {
1542 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001543 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001544 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001545 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001546 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001547 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001548 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001549 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001550 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001551 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001552 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001553 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001554 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001555 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001556 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001557 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001558 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001559 return( 128 );
1560 default:
1561 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001562 }
1563}
Gilles Peskinea05219c2018-11-16 16:02:56 +01001564#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001565
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001566/* Initialize the MAC operation structure. Once this function has been
1567 * called, psa_mac_abort can run and will do the right thing. */
1568static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1569 psa_algorithm_t alg )
1570{
1571 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1572
1573 operation->alg = alg;
1574 operation->key_set = 0;
1575 operation->iv_set = 0;
1576 operation->iv_required = 0;
1577 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001578 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001579
1580#if defined(MBEDTLS_CMAC_C)
1581 if( alg == PSA_ALG_CMAC )
1582 {
1583 operation->iv_required = 0;
1584 mbedtls_cipher_init( &operation->ctx.cmac );
1585 status = PSA_SUCCESS;
1586 }
1587 else
1588#endif /* MBEDTLS_CMAC_C */
1589#if defined(MBEDTLS_MD_C)
1590 if( PSA_ALG_IS_HMAC( operation->alg ) )
1591 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001592 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1593 operation->ctx.hmac.hash_ctx.alg = 0;
1594 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001595 }
1596 else
1597#endif /* MBEDTLS_MD_C */
1598 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001599 if( ! PSA_ALG_IS_MAC( alg ) )
1600 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001601 }
1602
1603 if( status != PSA_SUCCESS )
1604 memset( operation, 0, sizeof( *operation ) );
1605 return( status );
1606}
1607
Gilles Peskine01126fa2018-07-12 17:04:55 +02001608#if defined(MBEDTLS_MD_C)
1609static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1610{
1611 mbedtls_zeroize( hmac->opad, sizeof( hmac->opad ) );
1612 return( psa_hash_abort( &hmac->hash_ctx ) );
1613}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01001614
1615static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
1616{
1617 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
1618 memset( hmac, 0, sizeof( *hmac ) );
1619}
Gilles Peskine01126fa2018-07-12 17:04:55 +02001620#endif /* MBEDTLS_MD_C */
1621
Gilles Peskine8c9def32018-02-08 10:02:12 +01001622psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1623{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001624 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001625 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001626 /* The object has (apparently) been initialized but it is not
1627 * in use. It's ok to call abort on such an object, and there's
1628 * nothing to do. */
1629 return( PSA_SUCCESS );
1630 }
1631 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001632#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001633 if( operation->alg == PSA_ALG_CMAC )
1634 {
1635 mbedtls_cipher_free( &operation->ctx.cmac );
1636 }
1637 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001638#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001639#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001640 if( PSA_ALG_IS_HMAC( operation->alg ) )
1641 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001642 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001643 }
1644 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001645#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001646 {
1647 /* Sanity check (shouldn't happen: operation->alg should
1648 * always have been initialized to a valid value). */
1649 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001650 }
Moran Peker41deec42018-04-04 15:43:05 +03001651
Gilles Peskine8c9def32018-02-08 10:02:12 +01001652 operation->alg = 0;
1653 operation->key_set = 0;
1654 operation->iv_set = 0;
1655 operation->iv_required = 0;
1656 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001657 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001658
Gilles Peskine8c9def32018-02-08 10:02:12 +01001659 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001660
1661bad_state:
1662 /* If abort is called on an uninitialized object, we can't trust
1663 * anything. Wipe the object in case it contains confidential data.
1664 * This may result in a memory leak if a pointer gets overwritten,
1665 * but it's too late to do anything about this. */
1666 memset( operation, 0, sizeof( *operation ) );
1667 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001668}
1669
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001670#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001671static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001672 size_t key_bits,
1673 key_slot_t *slot,
1674 const mbedtls_cipher_info_t *cipher_info )
1675{
1676 int ret;
1677
1678 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001679
1680 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1681 if( ret != 0 )
1682 return( ret );
1683
1684 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1685 slot->data.raw.data,
1686 key_bits );
1687 return( ret );
1688}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001689#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001690
Gilles Peskine248051a2018-06-20 16:09:38 +02001691#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001692static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1693 const uint8_t *key,
1694 size_t key_length,
1695 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001696{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001697 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001698 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001699 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001700 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001701 psa_status_t status;
1702
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001703 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1704 * overflow below. This should never trigger if the hash algorithm
1705 * is implemented correctly. */
1706 /* The size checks against the ipad and opad buffers cannot be written
1707 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1708 * because that triggers -Wlogical-op on GCC 7.3. */
1709 if( block_size > sizeof( ipad ) )
1710 return( PSA_ERROR_NOT_SUPPORTED );
1711 if( block_size > sizeof( hmac->opad ) )
1712 return( PSA_ERROR_NOT_SUPPORTED );
1713 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001714 return( PSA_ERROR_NOT_SUPPORTED );
1715
Gilles Peskined223b522018-06-11 18:12:58 +02001716 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001717 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001718 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001719 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001720 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001721 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001722 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001723 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001724 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001725 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001726 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001727 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001728 }
Gilles Peskine96889972018-07-12 17:07:03 +02001729 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1730 * but it is permitted. It is common when HMAC is used in HKDF, for
1731 * example. Don't call `memcpy` in the 0-length because `key` could be
1732 * an invalid pointer which would make the behavior undefined. */
1733 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001734 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001735
Gilles Peskined223b522018-06-11 18:12:58 +02001736 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1737 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001738 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001739 ipad[i] ^= 0x36;
1740 memset( ipad + key_length, 0x36, block_size - key_length );
1741
1742 /* Copy the key material from ipad to opad, flipping the requisite bits,
1743 * and filling the rest of opad with the requisite constant. */
1744 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001745 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1746 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001747
Gilles Peskine01126fa2018-07-12 17:04:55 +02001748 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001749 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001750 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001751
Gilles Peskine01126fa2018-07-12 17:04:55 +02001752 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001753
1754cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001755 mbedtls_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001756
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001757 return( status );
1758}
Gilles Peskine248051a2018-06-20 16:09:38 +02001759#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001760
Gilles Peskine89167cb2018-07-08 20:12:23 +02001761static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
1762 psa_key_slot_t key,
1763 psa_algorithm_t alg,
1764 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001765{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001766 psa_status_t status;
1767 key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001768 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001769 psa_key_usage_t usage =
1770 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02001771 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02001772 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001773
Gilles Peskined911eb72018-08-14 15:18:45 +02001774 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001775 if( status != PSA_SUCCESS )
1776 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001777 if( is_sign )
1778 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001779
Gilles Peskine89167cb2018-07-08 20:12:23 +02001780 status = psa_get_key_from_slot( key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001781 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001782 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001783 key_bits = psa_get_key_bits( slot );
1784
Gilles Peskine8c9def32018-02-08 10:02:12 +01001785#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001786 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001787 {
1788 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02001789 mbedtls_cipher_info_from_psa( full_length_alg,
1790 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001791 int ret;
1792 if( cipher_info == NULL )
1793 {
1794 status = PSA_ERROR_NOT_SUPPORTED;
1795 goto exit;
1796 }
1797 operation->mac_size = cipher_info->block_size;
1798 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
1799 status = mbedtls_to_psa_error( ret );
1800 }
1801 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001802#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001803#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001804 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001805 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02001806 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001807 if( hash_alg == 0 )
1808 {
1809 status = PSA_ERROR_NOT_SUPPORTED;
1810 goto exit;
1811 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001812
1813 operation->mac_size = PSA_HASH_SIZE( hash_alg );
1814 /* Sanity check. This shouldn't fail on a valid configuration. */
1815 if( operation->mac_size == 0 ||
1816 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
1817 {
1818 status = PSA_ERROR_NOT_SUPPORTED;
1819 goto exit;
1820 }
1821
Gilles Peskine01126fa2018-07-12 17:04:55 +02001822 if( slot->type != PSA_KEY_TYPE_HMAC )
1823 {
1824 status = PSA_ERROR_INVALID_ARGUMENT;
1825 goto exit;
1826 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001827
Gilles Peskine01126fa2018-07-12 17:04:55 +02001828 status = psa_hmac_setup_internal( &operation->ctx.hmac,
1829 slot->data.raw.data,
1830 slot->data.raw.bytes,
1831 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001832 }
1833 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001834#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001835 {
1836 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001837 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001838
Gilles Peskined911eb72018-08-14 15:18:45 +02001839 if( truncated == 0 )
1840 {
1841 /* The "normal" case: untruncated algorithm. Nothing to do. */
1842 }
1843 else if( truncated < 4 )
1844 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02001845 /* A very short MAC is too short for security since it can be
1846 * brute-forced. Ancient protocols with 32-bit MACs do exist,
1847 * so we make this our minimum, even though 32 bits is still
1848 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02001849 status = PSA_ERROR_NOT_SUPPORTED;
1850 }
1851 else if( truncated > operation->mac_size )
1852 {
1853 /* It's impossible to "truncate" to a larger length. */
1854 status = PSA_ERROR_INVALID_ARGUMENT;
1855 }
1856 else
1857 operation->mac_size = truncated;
1858
Gilles Peskinefbfac682018-07-08 20:51:54 +02001859exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001860 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001861 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001862 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001863 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001864 else
1865 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001866 operation->key_set = 1;
1867 }
1868 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001869}
1870
Gilles Peskine89167cb2018-07-08 20:12:23 +02001871psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
1872 psa_key_slot_t key,
1873 psa_algorithm_t alg )
1874{
1875 return( psa_mac_setup( operation, key, alg, 1 ) );
1876}
1877
1878psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
1879 psa_key_slot_t key,
1880 psa_algorithm_t alg )
1881{
1882 return( psa_mac_setup( operation, key, alg, 0 ) );
1883}
1884
Gilles Peskine8c9def32018-02-08 10:02:12 +01001885psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1886 const uint8_t *input,
1887 size_t input_length )
1888{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001889 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001890 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001891 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001892 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001893 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001894 operation->has_input = 1;
1895
Gilles Peskine8c9def32018-02-08 10:02:12 +01001896#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001897 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001898 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001899 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1900 input, input_length );
1901 status = mbedtls_to_psa_error( ret );
1902 }
1903 else
1904#endif /* MBEDTLS_CMAC_C */
1905#if defined(MBEDTLS_MD_C)
1906 if( PSA_ALG_IS_HMAC( operation->alg ) )
1907 {
1908 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
1909 input_length );
1910 }
1911 else
1912#endif /* MBEDTLS_MD_C */
1913 {
1914 /* This shouldn't happen if `operation` was initialized by
1915 * a setup function. */
1916 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001917 }
1918
Gilles Peskinefbfac682018-07-08 20:51:54 +02001919cleanup:
1920 if( status != PSA_SUCCESS )
1921 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001922 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001923}
1924
Gilles Peskine01126fa2018-07-12 17:04:55 +02001925#if defined(MBEDTLS_MD_C)
1926static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
1927 uint8_t *mac,
1928 size_t mac_size )
1929{
1930 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
1931 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
1932 size_t hash_size = 0;
1933 size_t block_size = psa_get_hash_block_size( hash_alg );
1934 psa_status_t status;
1935
Gilles Peskine01126fa2018-07-12 17:04:55 +02001936 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1937 if( status != PSA_SUCCESS )
1938 return( status );
1939 /* From here on, tmp needs to be wiped. */
1940
1941 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
1942 if( status != PSA_SUCCESS )
1943 goto exit;
1944
1945 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
1946 if( status != PSA_SUCCESS )
1947 goto exit;
1948
1949 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
1950 if( status != PSA_SUCCESS )
1951 goto exit;
1952
Gilles Peskined911eb72018-08-14 15:18:45 +02001953 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1954 if( status != PSA_SUCCESS )
1955 goto exit;
1956
1957 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001958
1959exit:
1960 mbedtls_zeroize( tmp, hash_size );
1961 return( status );
1962}
1963#endif /* MBEDTLS_MD_C */
1964
mohammad16036df908f2018-04-02 08:34:15 -07001965static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02001966 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001967 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001968{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02001969 if( ! operation->key_set )
1970 return( PSA_ERROR_BAD_STATE );
1971 if( operation->iv_required && ! operation->iv_set )
1972 return( PSA_ERROR_BAD_STATE );
1973
Gilles Peskine8c9def32018-02-08 10:02:12 +01001974 if( mac_size < operation->mac_size )
1975 return( PSA_ERROR_BUFFER_TOO_SMALL );
1976
Gilles Peskine8c9def32018-02-08 10:02:12 +01001977#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001978 if( operation->alg == PSA_ALG_CMAC )
1979 {
Gilles Peskined911eb72018-08-14 15:18:45 +02001980 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
1981 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
1982 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02001983 memcpy( mac, tmp, operation->mac_size );
Gilles Peskined911eb72018-08-14 15:18:45 +02001984 mbedtls_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001985 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001986 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02001987 else
1988#endif /* MBEDTLS_CMAC_C */
1989#if defined(MBEDTLS_MD_C)
1990 if( PSA_ALG_IS_HMAC( operation->alg ) )
1991 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001992 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02001993 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001994 }
1995 else
1996#endif /* MBEDTLS_MD_C */
1997 {
1998 /* This shouldn't happen if `operation` was initialized by
1999 * a setup function. */
2000 return( PSA_ERROR_BAD_STATE );
2001 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002002}
2003
Gilles Peskineacd4be32018-07-08 19:56:25 +02002004psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2005 uint8_t *mac,
2006 size_t mac_size,
2007 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002008{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002009 psa_status_t status;
2010
2011 /* Fill the output buffer with something that isn't a valid mac
2012 * (barring an attack on the mac and deliberately-crafted input),
2013 * in case the caller doesn't check the return status properly. */
2014 *mac_length = mac_size;
2015 /* If mac_size is 0 then mac may be NULL and then the
2016 * call to memset would have undefined behavior. */
2017 if( mac_size != 0 )
2018 memset( mac, '!', mac_size );
2019
Gilles Peskine89167cb2018-07-08 20:12:23 +02002020 if( ! operation->is_sign )
2021 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002022 status = PSA_ERROR_BAD_STATE;
2023 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002024 }
mohammad16036df908f2018-04-02 08:34:15 -07002025
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002026 status = psa_mac_finish_internal( operation, mac, mac_size );
2027
2028cleanup:
2029 if( status == PSA_SUCCESS )
2030 {
2031 status = psa_mac_abort( operation );
2032 if( status == PSA_SUCCESS )
2033 *mac_length = operation->mac_size;
2034 else
2035 memset( mac, '!', mac_size );
2036 }
2037 else
2038 psa_mac_abort( operation );
2039 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002040}
2041
Gilles Peskineacd4be32018-07-08 19:56:25 +02002042psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2043 const uint8_t *mac,
2044 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002045{
Gilles Peskine828ed142018-06-18 23:25:51 +02002046 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002047 psa_status_t status;
2048
Gilles Peskine89167cb2018-07-08 20:12:23 +02002049 if( operation->is_sign )
2050 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002051 status = PSA_ERROR_BAD_STATE;
2052 goto cleanup;
2053 }
2054 if( operation->mac_size != mac_length )
2055 {
2056 status = PSA_ERROR_INVALID_SIGNATURE;
2057 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002058 }
mohammad16036df908f2018-04-02 08:34:15 -07002059
2060 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002061 actual_mac, sizeof( actual_mac ) );
2062
2063 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2064 status = PSA_ERROR_INVALID_SIGNATURE;
2065
2066cleanup:
2067 if( status == PSA_SUCCESS )
2068 status = psa_mac_abort( operation );
2069 else
2070 psa_mac_abort( operation );
2071
Gilles Peskine99b7d6b2018-08-21 14:56:19 +02002072 mbedtls_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002073
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002074 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002075}
2076
2077
Gilles Peskine20035e32018-02-03 22:44:14 +01002078
Gilles Peskine20035e32018-02-03 22:44:14 +01002079/****************************************************************/
2080/* Asymmetric cryptography */
2081/****************************************************************/
2082
Gilles Peskine2b450e32018-06-27 15:42:46 +02002083#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002084/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002085 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002086static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2087 size_t hash_length,
2088 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002089{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002090 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002091 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002092 *md_alg = mbedtls_md_get_type( md_info );
2093
2094 /* The Mbed TLS RSA module uses an unsigned int for hash length
2095 * parameters. Validate that it fits so that we don't risk an
2096 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002097#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002098 if( hash_length > UINT_MAX )
2099 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002100#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002101
2102#if defined(MBEDTLS_PKCS1_V15)
2103 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2104 * must be correct. */
2105 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2106 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002107 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002108 if( md_info == NULL )
2109 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002110 if( mbedtls_md_get_size( md_info ) != hash_length )
2111 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002112 }
2113#endif /* MBEDTLS_PKCS1_V15 */
2114
2115#if defined(MBEDTLS_PKCS1_V21)
2116 /* PSS requires a hash internally. */
2117 if( PSA_ALG_IS_RSA_PSS( alg ) )
2118 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002119 if( md_info == NULL )
2120 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002121 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002122#endif /* MBEDTLS_PKCS1_V21 */
2123
Gilles Peskine61b91d42018-06-08 16:09:36 +02002124 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002125}
2126
Gilles Peskine2b450e32018-06-27 15:42:46 +02002127static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2128 psa_algorithm_t alg,
2129 const uint8_t *hash,
2130 size_t hash_length,
2131 uint8_t *signature,
2132 size_t signature_size,
2133 size_t *signature_length )
2134{
2135 psa_status_t status;
2136 int ret;
2137 mbedtls_md_type_t md_alg;
2138
2139 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2140 if( status != PSA_SUCCESS )
2141 return( status );
2142
Gilles Peskine630a18a2018-06-29 17:49:35 +02002143 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002144 return( PSA_ERROR_BUFFER_TOO_SMALL );
2145
2146#if defined(MBEDTLS_PKCS1_V15)
2147 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2148 {
2149 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2150 MBEDTLS_MD_NONE );
2151 ret = mbedtls_rsa_pkcs1_sign( rsa,
2152 mbedtls_ctr_drbg_random,
2153 &global_data.ctr_drbg,
2154 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002155 md_alg,
2156 (unsigned int) hash_length,
2157 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002158 signature );
2159 }
2160 else
2161#endif /* MBEDTLS_PKCS1_V15 */
2162#if defined(MBEDTLS_PKCS1_V21)
2163 if( PSA_ALG_IS_RSA_PSS( alg ) )
2164 {
2165 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2166 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2167 mbedtls_ctr_drbg_random,
2168 &global_data.ctr_drbg,
2169 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002170 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002171 (unsigned int) hash_length,
2172 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002173 signature );
2174 }
2175 else
2176#endif /* MBEDTLS_PKCS1_V21 */
2177 {
2178 return( PSA_ERROR_INVALID_ARGUMENT );
2179 }
2180
2181 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002182 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002183 return( mbedtls_to_psa_error( ret ) );
2184}
2185
2186static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2187 psa_algorithm_t alg,
2188 const uint8_t *hash,
2189 size_t hash_length,
2190 const uint8_t *signature,
2191 size_t signature_length )
2192{
2193 psa_status_t status;
2194 int ret;
2195 mbedtls_md_type_t md_alg;
2196
2197 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2198 if( status != PSA_SUCCESS )
2199 return( status );
2200
Gilles Peskine630a18a2018-06-29 17:49:35 +02002201 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002202 return( PSA_ERROR_BUFFER_TOO_SMALL );
2203
2204#if defined(MBEDTLS_PKCS1_V15)
2205 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2206 {
2207 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2208 MBEDTLS_MD_NONE );
2209 ret = mbedtls_rsa_pkcs1_verify( rsa,
2210 mbedtls_ctr_drbg_random,
2211 &global_data.ctr_drbg,
2212 MBEDTLS_RSA_PUBLIC,
2213 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002214 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002215 hash,
2216 signature );
2217 }
2218 else
2219#endif /* MBEDTLS_PKCS1_V15 */
2220#if defined(MBEDTLS_PKCS1_V21)
2221 if( PSA_ALG_IS_RSA_PSS( alg ) )
2222 {
2223 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2224 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2225 mbedtls_ctr_drbg_random,
2226 &global_data.ctr_drbg,
2227 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002228 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002229 (unsigned int) hash_length,
2230 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002231 signature );
2232 }
2233 else
2234#endif /* MBEDTLS_PKCS1_V21 */
2235 {
2236 return( PSA_ERROR_INVALID_ARGUMENT );
2237 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002238
2239 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2240 * the rest of the signature is invalid". This has little use in
2241 * practice and PSA doesn't report this distinction. */
2242 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2243 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002244 return( mbedtls_to_psa_error( ret ) );
2245}
2246#endif /* MBEDTLS_RSA_C */
2247
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002248#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002249/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2250 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2251 * (even though these functions don't modify it). */
2252static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2253 psa_algorithm_t alg,
2254 const uint8_t *hash,
2255 size_t hash_length,
2256 uint8_t *signature,
2257 size_t signature_size,
2258 size_t *signature_length )
2259{
2260 int ret;
2261 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002262 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002263 mbedtls_mpi_init( &r );
2264 mbedtls_mpi_init( &s );
2265
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002266 if( signature_size < 2 * curve_bytes )
2267 {
2268 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2269 goto cleanup;
2270 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002271
Gilles Peskinea05219c2018-11-16 16:02:56 +01002272#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002273 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2274 {
2275 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2276 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2277 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2278 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2279 hash, hash_length,
2280 md_alg ) );
2281 }
2282 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002283#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002284 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002285 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002286 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2287 hash, hash_length,
2288 mbedtls_ctr_drbg_random,
2289 &global_data.ctr_drbg ) );
2290 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002291
2292 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2293 signature,
2294 curve_bytes ) );
2295 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2296 signature + curve_bytes,
2297 curve_bytes ) );
2298
2299cleanup:
2300 mbedtls_mpi_free( &r );
2301 mbedtls_mpi_free( &s );
2302 if( ret == 0 )
2303 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002304 return( mbedtls_to_psa_error( ret ) );
2305}
2306
2307static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2308 const uint8_t *hash,
2309 size_t hash_length,
2310 const uint8_t *signature,
2311 size_t signature_length )
2312{
2313 int ret;
2314 mbedtls_mpi r, s;
2315 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2316 mbedtls_mpi_init( &r );
2317 mbedtls_mpi_init( &s );
2318
2319 if( signature_length != 2 * curve_bytes )
2320 return( PSA_ERROR_INVALID_SIGNATURE );
2321
2322 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2323 signature,
2324 curve_bytes ) );
2325 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2326 signature + curve_bytes,
2327 curve_bytes ) );
2328
2329 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2330 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002331
2332cleanup:
2333 mbedtls_mpi_free( &r );
2334 mbedtls_mpi_free( &s );
2335 return( mbedtls_to_psa_error( ret ) );
2336}
2337#endif /* MBEDTLS_ECDSA_C */
2338
Gilles Peskine61b91d42018-06-08 16:09:36 +02002339psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
2340 psa_algorithm_t alg,
2341 const uint8_t *hash,
2342 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002343 uint8_t *signature,
2344 size_t signature_size,
2345 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002346{
2347 key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002348 psa_status_t status;
2349
2350 *signature_length = signature_size;
2351
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002352 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_SIGN, alg );
2353 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002354 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002355 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002356 {
2357 status = PSA_ERROR_INVALID_ARGUMENT;
2358 goto exit;
2359 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002360
Gilles Peskine20035e32018-02-03 22:44:14 +01002361#if defined(MBEDTLS_RSA_C)
2362 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2363 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002364 status = psa_rsa_sign( slot->data.rsa,
2365 alg,
2366 hash, hash_length,
2367 signature, signature_size,
2368 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002369 }
2370 else
2371#endif /* defined(MBEDTLS_RSA_C) */
2372#if defined(MBEDTLS_ECP_C)
2373 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2374 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002375#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002376 if(
2377#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2378 PSA_ALG_IS_ECDSA( alg )
2379#else
2380 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2381#endif
2382 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002383 status = psa_ecdsa_sign( slot->data.ecp,
2384 alg,
2385 hash, hash_length,
2386 signature, signature_size,
2387 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002388 else
2389#endif /* defined(MBEDTLS_ECDSA_C) */
2390 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002391 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002392 }
itayzafrir5c753392018-05-08 11:18:38 +03002393 }
2394 else
2395#endif /* defined(MBEDTLS_ECP_C) */
2396 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002397 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002398 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002399
2400exit:
2401 /* Fill the unused part of the output buffer (the whole buffer on error,
2402 * the trailing part on success) with something that isn't a valid mac
2403 * (barring an attack on the mac and deliberately-crafted input),
2404 * in case the caller doesn't check the return status properly. */
2405 if( status == PSA_SUCCESS )
2406 memset( signature + *signature_length, '!',
2407 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002408 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002409 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002410 /* If signature_size is 0 then we have nothing to do. We must not call
2411 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002412 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002413}
2414
2415psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
2416 psa_algorithm_t alg,
2417 const uint8_t *hash,
2418 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002419 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002420 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002421{
2422 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002423 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002424
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002425 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_VERIFY, alg );
2426 if( status != PSA_SUCCESS )
2427 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002428
Gilles Peskine61b91d42018-06-08 16:09:36 +02002429#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002430 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002431 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002432 return( psa_rsa_verify( slot->data.rsa,
2433 alg,
2434 hash, hash_length,
2435 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002436 }
2437 else
2438#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002439#if defined(MBEDTLS_ECP_C)
2440 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2441 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002442#if defined(MBEDTLS_ECDSA_C)
2443 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002444 return( psa_ecdsa_verify( slot->data.ecp,
2445 hash, hash_length,
2446 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002447 else
2448#endif /* defined(MBEDTLS_ECDSA_C) */
2449 {
2450 return( PSA_ERROR_INVALID_ARGUMENT );
2451 }
itayzafrir5c753392018-05-08 11:18:38 +03002452 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002453 else
2454#endif /* defined(MBEDTLS_ECP_C) */
2455 {
2456 return( PSA_ERROR_NOT_SUPPORTED );
2457 }
2458}
2459
Gilles Peskine072ac562018-06-30 00:21:29 +02002460#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2461static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2462 mbedtls_rsa_context *rsa )
2463{
2464 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2465 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2466 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2467 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2468}
2469#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2470
Gilles Peskine61b91d42018-06-08 16:09:36 +02002471psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
2472 psa_algorithm_t alg,
2473 const uint8_t *input,
2474 size_t input_length,
2475 const uint8_t *salt,
2476 size_t salt_length,
2477 uint8_t *output,
2478 size_t output_size,
2479 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002480{
2481 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002482 psa_status_t status;
2483
Darryl Green5cc689a2018-07-24 15:34:10 +01002484 (void) input;
2485 (void) input_length;
2486 (void) salt;
2487 (void) output;
2488 (void) output_size;
2489
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002490 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002491
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002492 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2493 return( PSA_ERROR_INVALID_ARGUMENT );
2494
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002495 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2496 if( status != PSA_SUCCESS )
2497 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002498 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2499 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002500 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002501
2502#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002503 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002504 {
2505 mbedtls_rsa_context *rsa = slot->data.rsa;
2506 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002507 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002508 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002509#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002510 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002511 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002512 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2513 mbedtls_ctr_drbg_random,
2514 &global_data.ctr_drbg,
2515 MBEDTLS_RSA_PUBLIC,
2516 input_length,
2517 input,
2518 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002519 }
2520 else
2521#endif /* MBEDTLS_PKCS1_V15 */
2522#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002523 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002524 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002525 psa_rsa_oaep_set_padding_mode( alg, rsa );
2526 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2527 mbedtls_ctr_drbg_random,
2528 &global_data.ctr_drbg,
2529 MBEDTLS_RSA_PUBLIC,
2530 salt, salt_length,
2531 input_length,
2532 input,
2533 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002534 }
2535 else
2536#endif /* MBEDTLS_PKCS1_V21 */
2537 {
2538 return( PSA_ERROR_INVALID_ARGUMENT );
2539 }
2540 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002541 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002542 return( mbedtls_to_psa_error( ret ) );
2543 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002544 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002545#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002546 {
2547 return( PSA_ERROR_NOT_SUPPORTED );
2548 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002549}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002550
Gilles Peskine61b91d42018-06-08 16:09:36 +02002551psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
2552 psa_algorithm_t alg,
2553 const uint8_t *input,
2554 size_t input_length,
2555 const uint8_t *salt,
2556 size_t salt_length,
2557 uint8_t *output,
2558 size_t output_size,
2559 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002560{
2561 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002562 psa_status_t status;
2563
Darryl Green5cc689a2018-07-24 15:34:10 +01002564 (void) input;
2565 (void) input_length;
2566 (void) salt;
2567 (void) output;
2568 (void) output_size;
2569
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002570 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002571
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002572 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2573 return( PSA_ERROR_INVALID_ARGUMENT );
2574
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002575 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2576 if( status != PSA_SUCCESS )
2577 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002578 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2579 return( PSA_ERROR_INVALID_ARGUMENT );
2580
2581#if defined(MBEDTLS_RSA_C)
2582 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2583 {
2584 mbedtls_rsa_context *rsa = slot->data.rsa;
2585 int ret;
2586
Gilles Peskine630a18a2018-06-29 17:49:35 +02002587 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002588 return( PSA_ERROR_INVALID_ARGUMENT );
2589
2590#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002591 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002592 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002593 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2594 mbedtls_ctr_drbg_random,
2595 &global_data.ctr_drbg,
2596 MBEDTLS_RSA_PRIVATE,
2597 output_length,
2598 input,
2599 output,
2600 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002601 }
2602 else
2603#endif /* MBEDTLS_PKCS1_V15 */
2604#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002605 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002606 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002607 psa_rsa_oaep_set_padding_mode( alg, rsa );
2608 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2609 mbedtls_ctr_drbg_random,
2610 &global_data.ctr_drbg,
2611 MBEDTLS_RSA_PRIVATE,
2612 salt, salt_length,
2613 output_length,
2614 input,
2615 output,
2616 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002617 }
2618 else
2619#endif /* MBEDTLS_PKCS1_V21 */
2620 {
2621 return( PSA_ERROR_INVALID_ARGUMENT );
2622 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002623
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002624 return( mbedtls_to_psa_error( ret ) );
2625 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002626 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002627#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002628 {
2629 return( PSA_ERROR_NOT_SUPPORTED );
2630 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002631}
Gilles Peskine20035e32018-02-03 22:44:14 +01002632
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002633
2634
mohammad1603503973b2018-03-12 15:59:30 +02002635/****************************************************************/
2636/* Symmetric cryptography */
2637/****************************************************************/
2638
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002639/* Initialize the cipher operation structure. Once this function has been
2640 * called, psa_cipher_abort can run and will do the right thing. */
2641static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2642 psa_algorithm_t alg )
2643{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002644 if( ! PSA_ALG_IS_CIPHER( alg ) )
2645 {
2646 memset( operation, 0, sizeof( *operation ) );
2647 return( PSA_ERROR_INVALID_ARGUMENT );
2648 }
2649
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002650 operation->alg = alg;
2651 operation->key_set = 0;
2652 operation->iv_set = 0;
2653 operation->iv_required = 1;
2654 operation->iv_size = 0;
2655 operation->block_size = 0;
2656 mbedtls_cipher_init( &operation->ctx.cipher );
2657 return( PSA_SUCCESS );
2658}
2659
Gilles Peskinee553c652018-06-04 16:22:46 +02002660static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
2661 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02002662 psa_algorithm_t alg,
2663 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002664{
2665 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2666 psa_status_t status;
2667 key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002668 size_t key_bits;
2669 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002670 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2671 PSA_KEY_USAGE_ENCRYPT :
2672 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002673
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002674 status = psa_cipher_init( operation, alg );
2675 if( status != PSA_SUCCESS )
2676 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002677
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002678 status = psa_get_key_from_slot( key, &slot, usage, alg);
2679 if( status != PSA_SUCCESS )
2680 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002681 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002682
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002683 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002684 if( cipher_info == NULL )
2685 return( PSA_ERROR_NOT_SUPPORTED );
2686
mohammad1603503973b2018-03-12 15:59:30 +02002687 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002688 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002689 {
2690 psa_cipher_abort( operation );
2691 return( mbedtls_to_psa_error( ret ) );
2692 }
2693
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002694#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002695 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002696 {
2697 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2698 unsigned char keys[24];
2699 memcpy( keys, slot->data.raw.data, 16 );
2700 memcpy( keys + 16, slot->data.raw.data, 8 );
2701 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2702 keys,
2703 192, cipher_operation );
2704 }
2705 else
2706#endif
2707 {
2708 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2709 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002710 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002711 }
Moran Peker41deec42018-04-04 15:43:05 +03002712 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002713 {
2714 psa_cipher_abort( operation );
2715 return( mbedtls_to_psa_error( ret ) );
2716 }
2717
mohammad16038481e742018-03-18 13:57:31 +02002718#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002719 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002720 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002721 case PSA_ALG_CBC_NO_PADDING:
2722 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2723 MBEDTLS_PADDING_NONE );
2724 break;
2725 case PSA_ALG_CBC_PKCS7:
2726 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2727 MBEDTLS_PADDING_PKCS7 );
2728 break;
2729 default:
2730 /* The algorithm doesn't involve padding. */
2731 ret = 0;
2732 break;
2733 }
2734 if( ret != 0 )
2735 {
2736 psa_cipher_abort( operation );
2737 return( mbedtls_to_psa_error( ret ) );
mohammad16038481e742018-03-18 13:57:31 +02002738 }
2739#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2740
mohammad1603503973b2018-03-12 15:59:30 +02002741 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002742 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2743 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
2744 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02002745 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002746 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002747 }
mohammad1603503973b2018-03-12 15:59:30 +02002748
Moran Peker395db872018-05-31 14:07:14 +03002749 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002750}
2751
Gilles Peskinefe119512018-07-08 21:39:34 +02002752psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
2753 psa_key_slot_t key,
2754 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002755{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002756 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002757}
2758
Gilles Peskinefe119512018-07-08 21:39:34 +02002759psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
2760 psa_key_slot_t key,
2761 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002762{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002763 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002764}
2765
Gilles Peskinefe119512018-07-08 21:39:34 +02002766psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2767 unsigned char *iv,
2768 size_t iv_size,
2769 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002770{
itayzafrir534bd7c2018-08-02 13:56:32 +03002771 psa_status_t status;
2772 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002773 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002774 {
2775 status = PSA_ERROR_BAD_STATE;
2776 goto exit;
2777 }
Moran Peker41deec42018-04-04 15:43:05 +03002778 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002779 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002780 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03002781 goto exit;
2782 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002783 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2784 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002785 if( ret != 0 )
2786 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002787 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002788 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002789 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002790
mohammad16038481e742018-03-18 13:57:31 +02002791 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03002792 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002793
Moran Peker395db872018-05-31 14:07:14 +03002794exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03002795 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03002796 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002797 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002798}
2799
Gilles Peskinefe119512018-07-08 21:39:34 +02002800psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2801 const unsigned char *iv,
2802 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002803{
itayzafrir534bd7c2018-08-02 13:56:32 +03002804 psa_status_t status;
2805 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002806 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002807 {
2808 status = PSA_ERROR_BAD_STATE;
2809 goto exit;
2810 }
Moran Pekera28258c2018-05-29 16:25:04 +03002811 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002812 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002813 status = PSA_ERROR_INVALID_ARGUMENT;
2814 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03002815 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002816 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
2817 status = mbedtls_to_psa_error( ret );
2818exit:
2819 if( status == PSA_SUCCESS )
2820 operation->iv_set = 1;
2821 else
mohammad1603503973b2018-03-12 15:59:30 +02002822 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002823 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002824}
2825
Gilles Peskinee553c652018-06-04 16:22:46 +02002826psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2827 const uint8_t *input,
2828 size_t input_length,
2829 unsigned char *output,
2830 size_t output_size,
2831 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002832{
itayzafrir534bd7c2018-08-02 13:56:32 +03002833 psa_status_t status;
2834 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002835 size_t expected_output_size;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002836 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002837 {
2838 /* Take the unprocessed partial block left over from previous
2839 * update calls, if any, plus the input to this call. Remove
2840 * the last partial block, if any. You get the data that will be
2841 * output in this call. */
2842 expected_output_size =
2843 ( operation->ctx.cipher.unprocessed_len + input_length )
2844 / operation->block_size * operation->block_size;
2845 }
2846 else
2847 {
2848 expected_output_size = input_length;
2849 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002850
Gilles Peskine89d789c2018-06-04 17:17:16 +02002851 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03002852 {
2853 status = PSA_ERROR_BUFFER_TOO_SMALL;
2854 goto exit;
2855 }
mohammad160382759612018-03-12 18:16:40 +02002856
mohammad1603503973b2018-03-12 15:59:30 +02002857 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002858 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03002859 status = mbedtls_to_psa_error( ret );
2860exit:
2861 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02002862 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002863 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002864}
2865
Gilles Peskinee553c652018-06-04 16:22:46 +02002866psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2867 uint8_t *output,
2868 size_t output_size,
2869 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002870{
Janos Follath315b51c2018-07-09 16:04:51 +01002871 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
2872 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002873 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002874
mohammad1603503973b2018-03-12 15:59:30 +02002875 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002876 {
Janos Follath315b51c2018-07-09 16:04:51 +01002877 status = PSA_ERROR_BAD_STATE;
2878 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002879 }
2880 if( operation->iv_required && ! operation->iv_set )
2881 {
Janos Follath315b51c2018-07-09 16:04:51 +01002882 status = PSA_ERROR_BAD_STATE;
2883 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002884 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002885
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002886 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002887 operation->alg == PSA_ALG_CBC_NO_PADDING &&
2888 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03002889 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002890 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01002891 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002892 }
2893
Janos Follath315b51c2018-07-09 16:04:51 +01002894 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
2895 temp_output_buffer,
2896 output_length );
2897 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002898 {
Janos Follath315b51c2018-07-09 16:04:51 +01002899 status = mbedtls_to_psa_error( cipher_ret );
2900 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02002901 }
Janos Follath315b51c2018-07-09 16:04:51 +01002902
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002903 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01002904 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002905 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002906 memcpy( output, temp_output_buffer, *output_length );
2907 else
2908 {
Janos Follath315b51c2018-07-09 16:04:51 +01002909 status = PSA_ERROR_BUFFER_TOO_SMALL;
2910 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002911 }
mohammad1603503973b2018-03-12 15:59:30 +02002912
Janos Follath279ab8e2018-07-09 16:13:21 +01002913 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002914 status = psa_cipher_abort( operation );
2915
2916 return( status );
2917
2918error:
2919
2920 *output_length = 0;
2921
Janos Follath279ab8e2018-07-09 16:13:21 +01002922 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002923 (void) psa_cipher_abort( operation );
2924
2925 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002926}
2927
Gilles Peskinee553c652018-06-04 16:22:46 +02002928psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2929{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002930 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02002931 {
2932 /* The object has (apparently) been initialized but it is not
2933 * in use. It's ok to call abort on such an object, and there's
2934 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002935 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02002936 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002937
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002938 /* Sanity check (shouldn't happen: operation->alg should
2939 * always have been initialized to a valid value). */
2940 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
2941 return( PSA_ERROR_BAD_STATE );
2942
mohammad1603503973b2018-03-12 15:59:30 +02002943 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002944
Moran Peker41deec42018-04-04 15:43:05 +03002945 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002946 operation->key_set = 0;
2947 operation->iv_set = 0;
2948 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002949 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002950 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002951
Moran Peker395db872018-05-31 14:07:14 +03002952 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002953}
2954
Gilles Peskinea0655c32018-04-30 17:06:50 +02002955
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002956
mohammad16038cc1cee2018-03-28 01:21:33 +03002957/****************************************************************/
2958/* Key Policy */
2959/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002960
mohammad160327010052018-07-03 13:16:15 +03002961#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02002962void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002963{
Gilles Peskine803ce742018-06-18 16:07:14 +02002964 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03002965}
2966
Gilles Peskine2d277862018-06-18 15:41:12 +02002967void psa_key_policy_set_usage( psa_key_policy_t *policy,
2968 psa_key_usage_t usage,
2969 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03002970{
mohammad16034eed7572018-03-28 05:14:59 -07002971 policy->usage = usage;
2972 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03002973}
2974
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002975psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002976{
mohammad16036df908f2018-04-02 08:34:15 -07002977 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03002978}
2979
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002980psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002981{
mohammad16036df908f2018-04-02 08:34:15 -07002982 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03002983}
mohammad160327010052018-07-03 13:16:15 +03002984#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002985
Gilles Peskine2d277862018-06-18 15:41:12 +02002986psa_status_t psa_set_key_policy( psa_key_slot_t key,
2987 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002988{
2989 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002990 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002991
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002992 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002993 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002994
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002995 status = psa_get_empty_key_slot( key, &slot );
2996 if( status != PSA_SUCCESS )
2997 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03002998
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002999 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
3000 PSA_KEY_USAGE_ENCRYPT |
3001 PSA_KEY_USAGE_DECRYPT |
3002 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02003003 PSA_KEY_USAGE_VERIFY |
3004 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07003005 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03003006
mohammad16036df908f2018-04-02 08:34:15 -07003007 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003008
3009 return( PSA_SUCCESS );
3010}
3011
Gilles Peskine2d277862018-06-18 15:41:12 +02003012psa_status_t psa_get_key_policy( psa_key_slot_t key,
3013 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003014{
3015 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003016 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003017
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003018 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003019 return( PSA_ERROR_INVALID_ARGUMENT );
3020
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003021 status = psa_get_key_slot( key, &slot );
3022 if( status != PSA_SUCCESS )
3023 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003024
mohammad16036df908f2018-04-02 08:34:15 -07003025 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003026
3027 return( PSA_SUCCESS );
3028}
Gilles Peskine20035e32018-02-03 22:44:14 +01003029
Gilles Peskinea0655c32018-04-30 17:06:50 +02003030
3031
mohammad1603804cd712018-03-20 22:44:08 +02003032/****************************************************************/
3033/* Key Lifetime */
3034/****************************************************************/
3035
Gilles Peskine2d277862018-06-18 15:41:12 +02003036psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
3037 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003038{
3039 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003040 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003041
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003042 status = psa_get_key_slot( key, &slot );
3043 if( status != PSA_SUCCESS )
3044 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003045
mohammad1603804cd712018-03-20 22:44:08 +02003046 *lifetime = slot->lifetime;
3047
3048 return( PSA_SUCCESS );
3049}
3050
Gilles Peskine2d277862018-06-18 15:41:12 +02003051psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
Jaeden Amero65fb2362018-06-26 13:55:30 +01003052 psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003053{
3054 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003055 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003056
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003057 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
3058 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
Darryl Greend49a4992018-06-18 17:27:26 +01003059 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE )
mohammad1603ba178512018-03-21 04:35:20 -07003060 return( PSA_ERROR_INVALID_ARGUMENT );
3061
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003062 status = psa_get_empty_key_slot( key, &slot );
3063 if( status != PSA_SUCCESS )
3064 return( status );
mohammad1603804cd712018-03-20 22:44:08 +02003065
Darryl Greend49a4992018-06-18 17:27:26 +01003066 if( lifetime == PSA_KEY_LIFETIME_WRITE_ONCE )
mohammad1603ba178512018-03-21 04:35:20 -07003067 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003068
Darryl Greend49a4992018-06-18 17:27:26 +01003069#if !defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
3070 if( lifetime == PSA_KEY_LIFETIME_PERSISTENT )
3071 return( PSA_ERROR_NOT_SUPPORTED );
3072#endif
3073
mohammad1603060ad8a2018-03-20 14:28:38 -07003074 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02003075
3076 return( PSA_SUCCESS );
3077}
3078
Gilles Peskine20035e32018-02-03 22:44:14 +01003079
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003080
mohammad16035955c982018-04-26 00:53:03 +03003081/****************************************************************/
3082/* AEAD */
3083/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003084
Gilles Peskineedf9a652018-08-17 18:11:56 +02003085typedef struct
3086{
3087 key_slot_t *slot;
3088 const mbedtls_cipher_info_t *cipher_info;
3089 union
3090 {
3091#if defined(MBEDTLS_CCM_C)
3092 mbedtls_ccm_context ccm;
3093#endif /* MBEDTLS_CCM_C */
3094#if defined(MBEDTLS_GCM_C)
3095 mbedtls_gcm_context gcm;
3096#endif /* MBEDTLS_GCM_C */
3097 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003098 psa_algorithm_t core_alg;
3099 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003100 uint8_t tag_length;
3101} aead_operation_t;
3102
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003103static void psa_aead_abort( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003104{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003105 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003106 {
3107#if defined(MBEDTLS_CCM_C)
3108 case PSA_ALG_CCM:
3109 mbedtls_ccm_free( &operation->ctx.ccm );
3110 break;
3111#endif /* MBEDTLS_CCM_C */
3112#if defined(MBEDTLS_CCM_C)
3113 case PSA_ALG_GCM:
3114 mbedtls_gcm_free( &operation->ctx.gcm );
3115 break;
3116#endif /* MBEDTLS_GCM_C */
3117 }
3118}
3119
3120static psa_status_t psa_aead_setup( aead_operation_t *operation,
3121 psa_key_slot_t key,
3122 psa_key_usage_t usage,
3123 psa_algorithm_t alg )
3124{
3125 psa_status_t status;
3126 size_t key_bits;
3127 mbedtls_cipher_id_t cipher_id;
3128
3129 status = psa_get_key_from_slot( key, &operation->slot, usage, alg );
3130 if( status != PSA_SUCCESS )
3131 return( status );
3132
3133 key_bits = psa_get_key_bits( operation->slot );
3134
3135 operation->cipher_info =
3136 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3137 &cipher_id );
3138 if( operation->cipher_info == NULL )
3139 return( PSA_ERROR_NOT_SUPPORTED );
3140
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003141 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003142 {
3143#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003144 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3145 operation->core_alg = PSA_ALG_CCM;
3146 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003147 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3148 return( PSA_ERROR_INVALID_ARGUMENT );
3149 mbedtls_ccm_init( &operation->ctx.ccm );
3150 status = mbedtls_to_psa_error(
3151 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3152 operation->slot->data.raw.data,
3153 (unsigned int) key_bits ) );
3154 if( status != 0 )
3155 goto cleanup;
3156 break;
3157#endif /* MBEDTLS_CCM_C */
3158
3159#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003160 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3161 operation->core_alg = PSA_ALG_GCM;
3162 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003163 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3164 return( PSA_ERROR_INVALID_ARGUMENT );
3165 mbedtls_gcm_init( &operation->ctx.gcm );
3166 status = mbedtls_to_psa_error(
3167 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3168 operation->slot->data.raw.data,
3169 (unsigned int) key_bits ) );
3170 break;
3171#endif /* MBEDTLS_GCM_C */
3172
3173 default:
3174 return( PSA_ERROR_NOT_SUPPORTED );
3175 }
3176
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003177 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3178 {
3179 status = PSA_ERROR_INVALID_ARGUMENT;
3180 goto cleanup;
3181 }
3182 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3183 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3184 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3185 * In both cases, mbedtls_xxx will validate the tag length below. */
3186
Gilles Peskineedf9a652018-08-17 18:11:56 +02003187 return( PSA_SUCCESS );
3188
3189cleanup:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003190 psa_aead_abort( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003191 return( status );
3192}
3193
mohammad16035955c982018-04-26 00:53:03 +03003194psa_status_t psa_aead_encrypt( psa_key_slot_t key,
3195 psa_algorithm_t alg,
3196 const uint8_t *nonce,
3197 size_t nonce_length,
3198 const uint8_t *additional_data,
3199 size_t additional_data_length,
3200 const uint8_t *plaintext,
3201 size_t plaintext_length,
3202 uint8_t *ciphertext,
3203 size_t ciphertext_size,
3204 size_t *ciphertext_length )
3205{
mohammad16035955c982018-04-26 00:53:03 +03003206 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003207 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003208 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003209
mohammad1603f08a5502018-06-03 15:05:47 +03003210 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003211
Gilles Peskineedf9a652018-08-17 18:11:56 +02003212 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003213 if( status != PSA_SUCCESS )
3214 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003215
Gilles Peskineedf9a652018-08-17 18:11:56 +02003216 /* For all currently supported modes, the tag is at the end of the
3217 * ciphertext. */
3218 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3219 {
3220 status = PSA_ERROR_BUFFER_TOO_SMALL;
3221 goto exit;
3222 }
3223 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003224
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003225 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003226 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003227 status = mbedtls_to_psa_error(
3228 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3229 MBEDTLS_GCM_ENCRYPT,
3230 plaintext_length,
3231 nonce, nonce_length,
3232 additional_data, additional_data_length,
3233 plaintext, ciphertext,
3234 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003235 }
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003236 else if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003237 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003238 status = mbedtls_to_psa_error(
3239 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3240 plaintext_length,
3241 nonce, nonce_length,
3242 additional_data,
3243 additional_data_length,
3244 plaintext, ciphertext,
3245 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003246 }
mohammad16035c8845f2018-05-09 05:40:09 -07003247 else
3248 {
mohammad1603554faad2018-06-03 15:07:38 +03003249 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003250 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003251
Gilles Peskineedf9a652018-08-17 18:11:56 +02003252 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3253 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003254
Gilles Peskineedf9a652018-08-17 18:11:56 +02003255exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003256 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003257 if( status == PSA_SUCCESS )
3258 *ciphertext_length = plaintext_length + operation.tag_length;
3259 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003260}
3261
Gilles Peskineee652a32018-06-01 19:23:52 +02003262/* Locate the tag in a ciphertext buffer containing the encrypted data
3263 * followed by the tag. Return the length of the part preceding the tag in
3264 * *plaintext_length. This is the size of the plaintext in modes where
3265 * the encrypted data has the same size as the plaintext, such as
3266 * CCM and GCM. */
3267static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3268 const uint8_t *ciphertext,
3269 size_t ciphertext_length,
3270 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003271 const uint8_t **p_tag )
3272{
3273 size_t payload_length;
3274 if( tag_length > ciphertext_length )
3275 return( PSA_ERROR_INVALID_ARGUMENT );
3276 payload_length = ciphertext_length - tag_length;
3277 if( payload_length > plaintext_size )
3278 return( PSA_ERROR_BUFFER_TOO_SMALL );
3279 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003280 return( PSA_SUCCESS );
3281}
3282
mohammad16035955c982018-04-26 00:53:03 +03003283psa_status_t psa_aead_decrypt( psa_key_slot_t key,
3284 psa_algorithm_t alg,
3285 const uint8_t *nonce,
3286 size_t nonce_length,
3287 const uint8_t *additional_data,
3288 size_t additional_data_length,
3289 const uint8_t *ciphertext,
3290 size_t ciphertext_length,
3291 uint8_t *plaintext,
3292 size_t plaintext_size,
3293 size_t *plaintext_length )
3294{
mohammad16035955c982018-04-26 00:53:03 +03003295 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003296 aead_operation_t operation;
3297 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003298
Gilles Peskineee652a32018-06-01 19:23:52 +02003299 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003300
Gilles Peskineedf9a652018-08-17 18:11:56 +02003301 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003302 if( status != PSA_SUCCESS )
3303 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003304
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003305 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003306 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003307 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003308 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003309 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003310 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003311 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003312
Gilles Peskineedf9a652018-08-17 18:11:56 +02003313 status = mbedtls_to_psa_error(
3314 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3315 ciphertext_length - operation.tag_length,
3316 nonce, nonce_length,
3317 additional_data,
3318 additional_data_length,
3319 tag, operation.tag_length,
3320 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003321 }
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003322 else if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003323 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003324 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003325 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003326 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003327 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003328 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003329
Gilles Peskineedf9a652018-08-17 18:11:56 +02003330 status = mbedtls_to_psa_error(
3331 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3332 ciphertext_length - operation.tag_length,
3333 nonce, nonce_length,
3334 additional_data,
3335 additional_data_length,
3336 ciphertext, plaintext,
3337 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003338 }
mohammad160339574652018-06-01 04:39:53 -07003339 else
3340 {
mohammad1603554faad2018-06-03 15:07:38 +03003341 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003342 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003343
Gilles Peskineedf9a652018-08-17 18:11:56 +02003344 if( status != PSA_SUCCESS && plaintext_size != 0 )
3345 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003346
Gilles Peskineedf9a652018-08-17 18:11:56 +02003347exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003348 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003349 if( status == PSA_SUCCESS )
3350 *plaintext_length = ciphertext_length - operation.tag_length;
3351 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003352}
3353
Gilles Peskinea0655c32018-04-30 17:06:50 +02003354
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003355
Gilles Peskine20035e32018-02-03 22:44:14 +01003356/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003357/* Generators */
3358/****************************************************************/
3359
3360psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3361{
3362 psa_status_t status = PSA_SUCCESS;
3363 if( generator->alg == 0 )
3364 {
3365 /* The object has (apparently) been initialized but it is not
3366 * in use. It's ok to call abort on such an object, and there's
3367 * nothing to do. */
3368 }
3369 else
Gilles Peskine751d9652018-09-18 12:05:44 +02003370 if( generator->alg == PSA_ALG_SELECT_RAW )
3371 {
3372 if( generator->ctx.buffer.data != NULL )
3373 {
3374 mbedtls_zeroize( generator->ctx.buffer.data,
3375 generator->ctx.buffer.size );
3376 mbedtls_free( generator->ctx.buffer.data );
3377 }
3378 }
3379 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003380#if defined(MBEDTLS_MD_C)
3381 if( PSA_ALG_IS_HKDF( generator->alg ) )
3382 {
3383 mbedtls_free( generator->ctx.hkdf.info );
3384 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3385 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003386 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3387 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
3388 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003389 {
3390 if( generator->ctx.tls12_prf.key != NULL )
3391 {
3392 mbedtls_zeroize( generator->ctx.tls12_prf.key,
3393 generator->ctx.tls12_prf.key_len );
3394 mbedtls_free( generator->ctx.tls12_prf.key );
3395 }
Hanno Becker580fba12018-11-13 20:50:45 +00003396
3397 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
3398 {
3399 mbedtls_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
3400 generator->ctx.tls12_prf.Ai_with_seed_len );
3401 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
3402 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003403 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003404 else
3405#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003406 {
3407 status = PSA_ERROR_BAD_STATE;
3408 }
3409 memset( generator, 0, sizeof( *generator ) );
3410 return( status );
3411}
3412
3413
3414psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3415 size_t *capacity)
3416{
3417 *capacity = generator->capacity;
3418 return( PSA_SUCCESS );
3419}
3420
Gilles Peskinebef7f142018-07-12 17:22:21 +02003421#if defined(MBEDTLS_MD_C)
3422/* Read some bytes from an HKDF-based generator. This performs a chunk
3423 * of the expand phase of the HKDF algorithm. */
3424static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3425 psa_algorithm_t hash_alg,
3426 uint8_t *output,
3427 size_t output_length )
3428{
3429 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3430 psa_status_t status;
3431
3432 while( output_length != 0 )
3433 {
3434 /* Copy what remains of the current block */
3435 uint8_t n = hash_length - hkdf->offset_in_block;
3436 if( n > output_length )
3437 n = (uint8_t) output_length;
3438 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3439 output += n;
3440 output_length -= n;
3441 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003442 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003443 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003444 /* We can't be wanting more output after block 0xff, otherwise
3445 * the capacity check in psa_generator_read() would have
3446 * prevented this call. It could happen only if the generator
3447 * object was corrupted or if this function is called directly
3448 * inside the library. */
3449 if( hkdf->block_number == 0xff )
3450 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003451
3452 /* We need a new block */
3453 ++hkdf->block_number;
3454 hkdf->offset_in_block = 0;
3455 status = psa_hmac_setup_internal( &hkdf->hmac,
3456 hkdf->prk, hash_length,
3457 hash_alg );
3458 if( status != PSA_SUCCESS )
3459 return( status );
3460 if( hkdf->block_number != 1 )
3461 {
3462 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3463 hkdf->output_block,
3464 hash_length );
3465 if( status != PSA_SUCCESS )
3466 return( status );
3467 }
3468 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3469 hkdf->info,
3470 hkdf->info_length );
3471 if( status != PSA_SUCCESS )
3472 return( status );
3473 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3474 &hkdf->block_number, 1 );
3475 if( status != PSA_SUCCESS )
3476 return( status );
3477 status = psa_hmac_finish_internal( &hkdf->hmac,
3478 hkdf->output_block,
3479 sizeof( hkdf->output_block ) );
3480 if( status != PSA_SUCCESS )
3481 return( status );
3482 }
3483
3484 return( PSA_SUCCESS );
3485}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003486
3487static psa_status_t psa_generator_tls12_prf_generate_next_block(
3488 psa_tls12_prf_generator_t *tls12_prf,
3489 psa_algorithm_t alg )
3490{
3491 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3492 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3493 psa_hmac_internal_data hmac;
3494 psa_status_t status, cleanup_status;
3495
Hanno Becker3b339e22018-11-13 20:56:14 +00003496 unsigned char *Ai;
3497 size_t Ai_len;
3498
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003499 /* We can't be wanting more output after block 0xff, otherwise
3500 * the capacity check in psa_generator_read() would have
3501 * prevented this call. It could happen only if the generator
3502 * object was corrupted or if this function is called directly
3503 * inside the library. */
3504 if( tls12_prf->block_number == 0xff )
3505 return( PSA_ERROR_BAD_STATE );
3506
3507 /* We need a new block */
3508 ++tls12_prf->block_number;
3509 tls12_prf->offset_in_block = 0;
3510
3511 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
3512 *
3513 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
3514 *
3515 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
3516 * HMAC_hash(secret, A(2) + seed) +
3517 * HMAC_hash(secret, A(3) + seed) + ...
3518 *
3519 * A(0) = seed
3520 * A(i) = HMAC_hash( secret, A(i-1) )
3521 *
3522 * The `psa_tls12_prf_generator` structures saves the block
3523 * `HMAC_hash(secret, A(i) + seed)` from which the output
3524 * is currently extracted as `output_block`, while
3525 * `A(i) + seed` is stored in `Ai_with_seed`.
3526 *
3527 * Generating a new block means recalculating `Ai_with_seed`
3528 * from the A(i)-part of it, and afterwards recalculating
3529 * `output_block`.
3530 *
3531 * A(0) is computed at setup time.
3532 *
3533 */
3534
3535 psa_hmac_init_internal( &hmac );
3536
3537 /* We must distinguish the calculation of A(1) from those
3538 * of A(2) and higher, because A(0)=seed has a different
3539 * length than the other A(i). */
3540 if( tls12_prf->block_number == 1 )
3541 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003542 Ai = tls12_prf->Ai_with_seed + hash_length;
3543 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003544 }
3545 else
3546 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003547 Ai = tls12_prf->Ai_with_seed;
3548 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003549 }
3550
Hanno Becker3b339e22018-11-13 20:56:14 +00003551 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
3552 status = psa_hmac_setup_internal( &hmac,
3553 tls12_prf->key,
3554 tls12_prf->key_len,
3555 hash_alg );
3556 if( status != PSA_SUCCESS )
3557 goto cleanup;
3558
3559 status = psa_hash_update( &hmac.hash_ctx,
3560 Ai, Ai_len );
3561 if( status != PSA_SUCCESS )
3562 goto cleanup;
3563
3564 status = psa_hmac_finish_internal( &hmac,
3565 tls12_prf->Ai_with_seed,
3566 hash_length );
3567 if( status != PSA_SUCCESS )
3568 goto cleanup;
3569
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003570 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
3571 status = psa_hmac_setup_internal( &hmac,
3572 tls12_prf->key,
3573 tls12_prf->key_len,
3574 hash_alg );
3575 if( status != PSA_SUCCESS )
3576 goto cleanup;
3577
3578 status = psa_hash_update( &hmac.hash_ctx,
3579 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003580 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003581 if( status != PSA_SUCCESS )
3582 goto cleanup;
3583
3584 status = psa_hmac_finish_internal( &hmac,
3585 tls12_prf->output_block,
3586 hash_length );
3587 if( status != PSA_SUCCESS )
3588 goto cleanup;
3589
3590cleanup:
3591
3592 cleanup_status = psa_hmac_abort_internal( &hmac );
3593 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
3594 status = cleanup_status;
3595
3596 return( status );
3597}
3598
3599/* Read some bytes from an TLS-1.2-PRF-based generator.
3600 * See Section 5 of RFC 5246. */
3601static psa_status_t psa_generator_tls12_prf_read(
3602 psa_tls12_prf_generator_t *tls12_prf,
3603 psa_algorithm_t alg,
3604 uint8_t *output,
3605 size_t output_length )
3606{
3607 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3608 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3609 psa_status_t status;
3610
3611 while( output_length != 0 )
3612 {
3613 /* Copy what remains of the current block */
3614 uint8_t n = hash_length - tls12_prf->offset_in_block;
3615
3616 /* Check if we have fully processed the current block. */
3617 if( n == 0 )
3618 {
3619 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
3620 alg );
3621 if( status != PSA_SUCCESS )
3622 return( status );
3623
3624 continue;
3625 }
3626
3627 if( n > output_length )
3628 n = (uint8_t) output_length;
3629 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
3630 n );
3631 output += n;
3632 output_length -= n;
3633 tls12_prf->offset_in_block += n;
3634 }
3635
3636 return( PSA_SUCCESS );
3637}
Gilles Peskinebef7f142018-07-12 17:22:21 +02003638#endif /* MBEDTLS_MD_C */
3639
Gilles Peskineeab56e42018-07-12 17:12:33 +02003640psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3641 uint8_t *output,
3642 size_t output_length )
3643{
3644 psa_status_t status;
3645
3646 if( output_length > generator->capacity )
3647 {
3648 generator->capacity = 0;
3649 /* Go through the error path to wipe all confidential data now
3650 * that the generator object is useless. */
3651 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3652 goto exit;
3653 }
3654 if( output_length == 0 &&
3655 generator->capacity == 0 && generator->alg == 0 )
3656 {
3657 /* Edge case: this is a blank or finished generator, and 0
3658 * bytes were requested. The right error in this case could
3659 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3660 * INSUFFICIENT_CAPACITY, which is right for a finished
3661 * generator, for consistency with the case when
3662 * output_length > 0. */
3663 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3664 }
3665 generator->capacity -= output_length;
3666
Gilles Peskine751d9652018-09-18 12:05:44 +02003667 if( generator->alg == PSA_ALG_SELECT_RAW )
3668 {
Gilles Peskine211a4362018-10-25 22:22:31 +02003669 /* Initially, the capacity of a selection generator is always
3670 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
3671 * abbreviated in this comment as `size`. When the remaining
3672 * capacity is `c`, the next bytes to serve start `c` bytes
3673 * from the end of the buffer, i.e. `size - c` from the
3674 * beginning of the buffer. Since `generator->capacity` was just
3675 * decremented above, we need to serve the bytes from
3676 * `size - generator->capacity - output_length` to
3677 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02003678 size_t offset =
3679 generator->ctx.buffer.size - generator->capacity - output_length;
3680 memcpy( output, generator->ctx.buffer.data + offset, output_length );
3681 status = PSA_SUCCESS;
3682 }
3683 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003684#if defined(MBEDTLS_MD_C)
3685 if( PSA_ALG_IS_HKDF( generator->alg ) )
3686 {
3687 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3688 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3689 output, output_length );
3690 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003691 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3692 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003693 {
3694 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
3695 generator->alg, output,
3696 output_length );
3697 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003698 else
3699#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003700 {
3701 return( PSA_ERROR_BAD_STATE );
3702 }
3703
3704exit:
3705 if( status != PSA_SUCCESS )
3706 {
3707 psa_generator_abort( generator );
3708 memset( output, '!', output_length );
3709 }
3710 return( status );
3711}
3712
Gilles Peskine08542d82018-07-19 17:05:42 +02003713#if defined(MBEDTLS_DES_C)
3714static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3715{
3716 if( data_size >= 8 )
3717 mbedtls_des_key_set_parity( data );
3718 if( data_size >= 16 )
3719 mbedtls_des_key_set_parity( data + 8 );
3720 if( data_size >= 24 )
3721 mbedtls_des_key_set_parity( data + 16 );
3722}
3723#endif /* MBEDTLS_DES_C */
3724
Gilles Peskineeab56e42018-07-12 17:12:33 +02003725psa_status_t psa_generator_import_key( psa_key_slot_t key,
3726 psa_key_type_t type,
3727 size_t bits,
3728 psa_crypto_generator_t *generator )
3729{
3730 uint8_t *data = NULL;
3731 size_t bytes = PSA_BITS_TO_BYTES( bits );
3732 psa_status_t status;
3733
3734 if( ! key_type_is_raw_bytes( type ) )
3735 return( PSA_ERROR_INVALID_ARGUMENT );
3736 if( bits % 8 != 0 )
3737 return( PSA_ERROR_INVALID_ARGUMENT );
3738 data = mbedtls_calloc( 1, bytes );
3739 if( data == NULL )
3740 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3741
3742 status = psa_generator_read( generator, data, bytes );
3743 if( status != PSA_SUCCESS )
3744 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003745#if defined(MBEDTLS_DES_C)
3746 if( type == PSA_KEY_TYPE_DES )
3747 psa_des_set_key_parity( data, bytes );
3748#endif /* MBEDTLS_DES_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003749 status = psa_import_key( key, type, data, bytes );
3750
3751exit:
3752 mbedtls_free( data );
3753 return( status );
3754}
3755
3756
3757
3758/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003759/* Key derivation */
3760/****************************************************************/
3761
Gilles Peskinea05219c2018-11-16 16:02:56 +01003762#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02003763/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01003764 * of the HKDF algorithm.
3765 *
3766 * Note that if this function fails, you must call psa_generator_abort()
3767 * to potentially free embedded data structures and wipe confidential data.
3768 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003769static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003770 const uint8_t *secret,
3771 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02003772 psa_algorithm_t hash_alg,
3773 const uint8_t *salt,
3774 size_t salt_length,
3775 const uint8_t *label,
3776 size_t label_length )
3777{
3778 psa_status_t status;
3779 status = psa_hmac_setup_internal( &hkdf->hmac,
3780 salt, salt_length,
Gilles Peskine00709fa2018-08-22 18:25:41 +02003781 PSA_ALG_HMAC_GET_HASH( hash_alg ) );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003782 if( status != PSA_SUCCESS )
3783 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003784 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003785 if( status != PSA_SUCCESS )
3786 return( status );
3787 status = psa_hmac_finish_internal( &hkdf->hmac,
3788 hkdf->prk,
3789 sizeof( hkdf->prk ) );
3790 if( status != PSA_SUCCESS )
3791 return( status );
3792 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
3793 hkdf->block_number = 0;
3794 hkdf->info_length = label_length;
3795 if( label_length != 0 )
3796 {
3797 hkdf->info = mbedtls_calloc( 1, label_length );
3798 if( hkdf->info == NULL )
3799 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3800 memcpy( hkdf->info, label, label_length );
3801 }
3802 return( PSA_SUCCESS );
3803}
Gilles Peskinea05219c2018-11-16 16:02:56 +01003804#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003805
Gilles Peskinea05219c2018-11-16 16:02:56 +01003806#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01003807/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
3808 *
3809 * Note that if this function fails, you must call psa_generator_abort()
3810 * to potentially free embedded data structures and wipe confidential data.
3811 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003812static psa_status_t psa_generator_tls12_prf_setup(
3813 psa_tls12_prf_generator_t *tls12_prf,
3814 const unsigned char *key,
3815 size_t key_len,
3816 psa_algorithm_t hash_alg,
3817 const uint8_t *salt,
3818 size_t salt_length,
3819 const uint8_t *label,
3820 size_t label_length )
3821{
3822 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00003823 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
3824 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003825
3826 tls12_prf->key = mbedtls_calloc( 1, key_len );
3827 if( tls12_prf->key == NULL )
3828 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3829 tls12_prf->key_len = key_len;
3830 memcpy( tls12_prf->key, key, key_len );
3831
Hanno Becker580fba12018-11-13 20:50:45 +00003832 overflow = ( salt_length + label_length < salt_length ) ||
3833 ( salt_length + label_length + hash_length < hash_length );
3834 if( overflow )
3835 return( PSA_ERROR_INVALID_ARGUMENT );
3836
3837 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
3838 if( tls12_prf->Ai_with_seed == NULL )
3839 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3840 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
3841
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003842 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
3843 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00003844 if( label_length != 0 )
3845 {
3846 memcpy( tls12_prf->Ai_with_seed + hash_length,
3847 label, label_length );
3848 }
3849
3850 if( salt_length != 0 )
3851 {
3852 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
3853 salt, salt_length );
3854 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003855
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003856 /* The first block gets generated when
3857 * psa_generator_read() is called. */
3858 tls12_prf->block_number = 0;
3859 tls12_prf->offset_in_block = hash_length;
3860
3861 return( PSA_SUCCESS );
3862}
Hanno Becker1aaedc02018-11-16 11:35:34 +00003863
3864/* Set up a TLS-1.2-PSK-to-MS-based generator. */
3865static psa_status_t psa_generator_tls12_psk_to_ms_setup(
3866 psa_tls12_prf_generator_t *tls12_prf,
3867 const unsigned char *psk,
3868 size_t psk_len,
3869 psa_algorithm_t hash_alg,
3870 const uint8_t *salt,
3871 size_t salt_length,
3872 const uint8_t *label,
3873 size_t label_length )
3874{
3875 psa_status_t status;
3876 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
3877
3878 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
3879 return( PSA_ERROR_INVALID_ARGUMENT );
3880
3881 /* Quoting RFC 4279, Section 2:
3882 *
3883 * The premaster secret is formed as follows: if the PSK is N octets
3884 * long, concatenate a uint16 with the value N, N zero octets, a second
3885 * uint16 with the value N, and the PSK itself.
3886 */
3887
3888 pms[0] = ( psk_len >> 8 ) & 0xff;
3889 pms[1] = ( psk_len >> 0 ) & 0xff;
3890 memset( pms + 2, 0, psk_len );
3891 pms[2 + psk_len + 0] = pms[0];
3892 pms[2 + psk_len + 1] = pms[1];
3893 memcpy( pms + 4 + psk_len, psk, psk_len );
3894
3895 status = psa_generator_tls12_prf_setup( tls12_prf,
3896 pms, 4 + 2 * psk_len,
3897 hash_alg,
3898 salt, salt_length,
3899 label, label_length );
3900
3901 mbedtls_zeroize( pms, sizeof( pms ) );
3902 return( status );
3903}
Gilles Peskinea05219c2018-11-16 16:02:56 +01003904#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003905
Gilles Peskine346797d2018-11-16 16:05:06 +01003906/* Note that if this function fails, you must call psa_generator_abort()
3907 * to potentially free embedded data structures and wipe confidential data.
3908 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003909static psa_status_t psa_key_derivation_internal(
3910 psa_crypto_generator_t *generator,
3911 const uint8_t *secret, size_t secret_length,
3912 psa_algorithm_t alg,
3913 const uint8_t *salt, size_t salt_length,
3914 const uint8_t *label, size_t label_length,
3915 size_t capacity )
3916{
3917 psa_status_t status;
3918 size_t max_capacity;
3919
3920 /* Set generator->alg even on failure so that abort knows what to do. */
3921 generator->alg = alg;
3922
Gilles Peskine751d9652018-09-18 12:05:44 +02003923 if( alg == PSA_ALG_SELECT_RAW )
3924 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003925 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02003926 if( salt_length != 0 )
3927 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01003928 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02003929 if( label_length != 0 )
3930 return( PSA_ERROR_INVALID_ARGUMENT );
3931 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
3932 if( generator->ctx.buffer.data == NULL )
3933 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3934 memcpy( generator->ctx.buffer.data, secret, secret_length );
3935 generator->ctx.buffer.size = secret_length;
3936 max_capacity = secret_length;
3937 status = PSA_SUCCESS;
3938 }
3939 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003940#if defined(MBEDTLS_MD_C)
3941 if( PSA_ALG_IS_HKDF( alg ) )
3942 {
3943 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3944 size_t hash_size = PSA_HASH_SIZE( hash_alg );
3945 if( hash_size == 0 )
3946 return( PSA_ERROR_NOT_SUPPORTED );
3947 max_capacity = 255 * hash_size;
3948 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
3949 secret, secret_length,
3950 hash_alg,
3951 salt, salt_length,
3952 label, label_length );
3953 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003954 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
3955 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
3956 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003957 {
3958 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3959 size_t hash_size = PSA_HASH_SIZE( hash_alg );
3960
3961 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
3962 if( hash_alg != PSA_ALG_SHA_256 &&
3963 hash_alg != PSA_ALG_SHA_384 )
3964 {
3965 return( PSA_ERROR_NOT_SUPPORTED );
3966 }
3967
3968 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00003969
3970 if( PSA_ALG_IS_TLS12_PRF( alg ) )
3971 {
3972 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
3973 secret, secret_length,
3974 hash_alg, salt, salt_length,
3975 label, label_length );
3976 }
3977 else
3978 {
3979 status = psa_generator_tls12_psk_to_ms_setup(
3980 &generator->ctx.tls12_prf,
3981 secret, secret_length,
3982 hash_alg, salt, salt_length,
3983 label, label_length );
3984 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003985 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003986 else
3987#endif
3988 {
3989 return( PSA_ERROR_NOT_SUPPORTED );
3990 }
3991
3992 if( status != PSA_SUCCESS )
3993 return( status );
3994
3995 if( capacity <= max_capacity )
3996 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02003997 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
3998 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003999 else
4000 return( PSA_ERROR_INVALID_ARGUMENT );
4001
4002 return( PSA_SUCCESS );
4003}
4004
Gilles Peskineea0fb492018-07-12 17:17:20 +02004005psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Darryl Green88001362018-07-26 13:59:04 +01004006 psa_key_slot_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004007 psa_algorithm_t alg,
4008 const uint8_t *salt,
4009 size_t salt_length,
4010 const uint8_t *label,
4011 size_t label_length,
4012 size_t capacity )
4013{
4014 key_slot_t *slot;
4015 psa_status_t status;
4016
4017 if( generator->alg != 0 )
4018 return( PSA_ERROR_BAD_STATE );
4019
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004020 /* Make sure that alg is a key derivation algorithm. This prevents
4021 * key selection algorithms, which psa_key_derivation_internal
4022 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004023 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4024 return( PSA_ERROR_INVALID_ARGUMENT );
4025
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004026 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DERIVE, alg );
4027 if( status != PSA_SUCCESS )
4028 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004029
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004030 if( slot->type != PSA_KEY_TYPE_DERIVE )
4031 return( PSA_ERROR_INVALID_ARGUMENT );
4032
4033 status = psa_key_derivation_internal( generator,
4034 slot->data.raw.data,
4035 slot->data.raw.bytes,
4036 alg,
4037 salt, salt_length,
4038 label, label_length,
4039 capacity );
4040 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004041 psa_generator_abort( generator );
4042 return( status );
4043}
4044
4045
4046
4047/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004048/* Key agreement */
4049/****************************************************************/
4050
Gilles Peskinea05219c2018-11-16 16:02:56 +01004051#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004052static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4053 size_t peer_key_length,
4054 const mbedtls_ecp_keypair *our_key,
4055 uint8_t *shared_secret,
4056 size_t shared_secret_size,
4057 size_t *shared_secret_length )
4058{
4059 mbedtls_pk_context pk;
4060 mbedtls_ecp_keypair *their_key = NULL;
4061 mbedtls_ecdh_context ecdh;
4062 int ret;
4063 mbedtls_ecdh_init( &ecdh );
4064 mbedtls_pk_init( &pk );
4065
4066 ret = mbedtls_pk_parse_public_key( &pk, peer_key, peer_key_length );
4067 if( ret != 0 )
4068 goto exit;
Gilles Peskine88714d72018-10-25 23:07:25 +02004069 switch( mbedtls_pk_get_type( &pk ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004070 {
Gilles Peskine88714d72018-10-25 23:07:25 +02004071 case MBEDTLS_PK_ECKEY:
4072 case MBEDTLS_PK_ECKEY_DH:
4073 break;
4074 default:
4075 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
4076 goto exit;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004077 }
4078 their_key = mbedtls_pk_ec( pk );
Gilles Peskineb4086612018-11-14 20:51:23 +01004079 if( their_key->grp.id != our_key->grp.id )
4080 {
4081 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
4082 goto exit;
4083 }
4084
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004085 ret = mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS );
4086 if( ret != 0 )
4087 goto exit;
4088 ret = mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS );
4089 if( ret != 0 )
4090 goto exit;
4091
4092 ret = mbedtls_ecdh_calc_secret( &ecdh,
4093 shared_secret_length,
4094 shared_secret, shared_secret_size,
4095 mbedtls_ctr_drbg_random,
4096 &global_data.ctr_drbg );
4097
4098exit:
4099 mbedtls_pk_free( &pk );
4100 mbedtls_ecdh_free( &ecdh );
4101 return( mbedtls_to_psa_error( ret ) );
4102}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004103#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004104
Gilles Peskine01d718c2018-09-18 12:01:02 +02004105#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4106
Gilles Peskine346797d2018-11-16 16:05:06 +01004107/* Note that if this function fails, you must call psa_generator_abort()
4108 * to potentially free embedded data structures and wipe confidential data.
4109 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004110static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
4111 key_slot_t *private_key,
4112 const uint8_t *peer_key,
4113 size_t peer_key_length,
4114 psa_algorithm_t alg )
4115{
4116 psa_status_t status;
4117 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4118 size_t shared_secret_length = 0;
4119
4120 /* Step 1: run the secret agreement algorithm to generate the shared
4121 * secret. */
4122 switch( PSA_ALG_KEY_AGREEMENT_GET_BASE( alg ) )
4123 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004124#if defined(MBEDTLS_ECDH_C)
4125 case PSA_ALG_ECDH_BASE:
4126 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
4127 return( PSA_ERROR_INVALID_ARGUMENT );
4128 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
4129 private_key->data.ecp,
4130 shared_secret,
4131 sizeof( shared_secret ),
4132 &shared_secret_length );
4133 break;
4134#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004135 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01004136 (void) private_key;
4137 (void) peer_key;
4138 (void) peer_key_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004139 return( PSA_ERROR_NOT_SUPPORTED );
4140 }
4141 if( status != PSA_SUCCESS )
4142 goto exit;
4143
4144 /* Step 2: set up the key derivation to generate key material from
4145 * the shared secret. */
4146 status = psa_key_derivation_internal( generator,
4147 shared_secret, shared_secret_length,
4148 PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ),
4149 NULL, 0, NULL, 0,
4150 PSA_GENERATOR_UNBRIDLED_CAPACITY );
4151exit:
4152 mbedtls_zeroize( shared_secret, shared_secret_length );
4153 return( status );
4154}
4155
4156psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
4157 psa_key_slot_t private_key,
4158 const uint8_t *peer_key,
4159 size_t peer_key_length,
4160 psa_algorithm_t alg )
4161{
4162 key_slot_t *slot;
4163 psa_status_t status;
4164 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
4165 return( PSA_ERROR_INVALID_ARGUMENT );
4166 status = psa_get_key_from_slot( private_key, &slot,
4167 PSA_KEY_USAGE_DERIVE, alg );
4168 if( status != PSA_SUCCESS )
4169 return( status );
Gilles Peskine346797d2018-11-16 16:05:06 +01004170 status = psa_key_agreement_internal( generator,
4171 slot,
4172 peer_key, peer_key_length,
4173 alg );
4174 if( status != PSA_SUCCESS )
4175 psa_generator_abort( generator );
4176 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004177}
4178
4179
4180
4181/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004182/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02004183/****************************************************************/
4184
4185psa_status_t psa_generate_random( uint8_t *output,
4186 size_t output_size )
4187{
itayzafrir0adf0fc2018-09-06 16:24:41 +03004188 int ret;
4189 GUARD_MODULE_INITIALIZED;
4190
4191 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02004192 return( mbedtls_to_psa_error( ret ) );
4193}
4194
4195psa_status_t psa_generate_key( psa_key_slot_t key,
4196 psa_key_type_t type,
4197 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02004198 const void *extra,
4199 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02004200{
Gilles Peskine12313cd2018-06-20 00:20:32 +02004201 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004202 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004203
Gilles Peskine53d991e2018-07-12 01:14:59 +02004204 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004205 return( PSA_ERROR_INVALID_ARGUMENT );
4206
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004207 status = psa_get_empty_key_slot( key, &slot );
4208 if( status != PSA_SUCCESS )
4209 return( status );
4210
Gilles Peskine48c0ea12018-06-21 14:15:31 +02004211 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004212 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004213 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004214 if( status != PSA_SUCCESS )
4215 return( status );
4216 status = psa_generate_random( slot->data.raw.data,
4217 slot->data.raw.bytes );
4218 if( status != PSA_SUCCESS )
4219 {
4220 mbedtls_free( slot->data.raw.data );
4221 return( status );
4222 }
4223#if defined(MBEDTLS_DES_C)
4224 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004225 psa_des_set_key_parity( slot->data.raw.data,
4226 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004227#endif /* MBEDTLS_DES_C */
4228 }
4229 else
4230
4231#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
4232 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
4233 {
4234 mbedtls_rsa_context *rsa;
4235 int ret;
4236 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02004237 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
4238 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01004239 /* Accept only byte-aligned keys, for the same reasons as
4240 * in psa_import_rsa_key(). */
4241 if( bits % 8 != 0 )
4242 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02004243 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004244 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02004245 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004246 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004247 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02004248#if INT_MAX < 0xffffffff
4249 /* Check that the uint32_t value passed by the caller fits
4250 * in the range supported by this implementation. */
4251 if( p->e > INT_MAX )
4252 return( PSA_ERROR_NOT_SUPPORTED );
4253#endif
4254 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004255 }
4256 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
4257 if( rsa == NULL )
4258 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4259 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
4260 ret = mbedtls_rsa_gen_key( rsa,
4261 mbedtls_ctr_drbg_random,
4262 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004263 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02004264 exponent );
4265 if( ret != 0 )
4266 {
4267 mbedtls_rsa_free( rsa );
4268 mbedtls_free( rsa );
4269 return( mbedtls_to_psa_error( ret ) );
4270 }
4271 slot->data.rsa = rsa;
4272 }
4273 else
4274#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
4275
4276#if defined(MBEDTLS_ECP_C)
4277 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
4278 {
4279 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
4280 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
4281 const mbedtls_ecp_curve_info *curve_info =
4282 mbedtls_ecp_curve_info_from_grp_id( grp_id );
4283 mbedtls_ecp_keypair *ecp;
4284 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004285 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004286 return( PSA_ERROR_NOT_SUPPORTED );
4287 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
4288 return( PSA_ERROR_NOT_SUPPORTED );
4289 if( curve_info->bit_size != bits )
4290 return( PSA_ERROR_INVALID_ARGUMENT );
4291 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
4292 if( ecp == NULL )
4293 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4294 mbedtls_ecp_keypair_init( ecp );
4295 ret = mbedtls_ecp_gen_key( grp_id, ecp,
4296 mbedtls_ctr_drbg_random,
4297 &global_data.ctr_drbg );
4298 if( ret != 0 )
4299 {
4300 mbedtls_ecp_keypair_free( ecp );
4301 mbedtls_free( ecp );
4302 return( mbedtls_to_psa_error( ret ) );
4303 }
4304 slot->data.ecp = ecp;
4305 }
4306 else
4307#endif /* MBEDTLS_ECP_C */
4308
4309 return( PSA_ERROR_NOT_SUPPORTED );
4310
4311 slot->type = type;
4312 return( PSA_SUCCESS );
Gilles Peskine05d69892018-06-19 22:00:52 +02004313}
4314
4315
4316/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01004317/* Module setup */
4318/****************************************************************/
4319
Gilles Peskinee59236f2018-01-27 23:32:46 +01004320void mbedtls_psa_crypto_free( void )
4321{
Jaeden Amero045bd502018-06-26 14:00:08 +01004322 psa_key_slot_t key;
Darryl Green40225ba2018-11-15 14:48:15 +00004323 key_slot_t *slot;
4324 psa_status_t status;
4325
Gilles Peskine9a056342018-08-01 15:46:54 +02004326 for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
Darryl Green40225ba2018-11-15 14:48:15 +00004327 {
4328 status = psa_get_key_slot( key, &slot );
4329 if( status != PSA_SUCCESS )
4330 continue;
4331 psa_remove_key_data_from_memory( slot );
4332 /* Zeroize the slot to wipe metadata such as policies. */
4333 mbedtls_zeroize( slot, sizeof( *slot ) );
4334 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01004335 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
4336 mbedtls_entropy_free( &global_data.entropy );
4337 mbedtls_zeroize( &global_data, sizeof( global_data ) );
4338}
4339
4340psa_status_t psa_crypto_init( void )
4341{
4342 int ret;
4343 const unsigned char drbg_seed[] = "PSA";
4344
4345 if( global_data.initialized != 0 )
4346 return( PSA_SUCCESS );
4347
4348 mbedtls_zeroize( &global_data, sizeof( global_data ) );
4349 mbedtls_entropy_init( &global_data.entropy );
4350 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
4351
4352 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
4353 mbedtls_entropy_func,
4354 &global_data.entropy,
4355 drbg_seed, sizeof( drbg_seed ) - 1 );
4356 if( ret != 0 )
4357 goto exit;
4358
Gilles Peskinee4ebc122018-03-07 14:16:44 +01004359 global_data.initialized = 1;
4360
Gilles Peskinee59236f2018-01-27 23:32:46 +01004361exit:
4362 if( ret != 0 )
4363 mbedtls_psa_crypto_free( );
4364 return( mbedtls_to_psa_error( ret ) );
4365}
4366
4367#endif /* MBEDTLS_PSA_CRYPTO_C */