blob: 50c8a8962b687dd739e70aafb0262f4247d6050b [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
4/* Copyright (C) 2018, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_PSA_CRYPTO_C)
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +030029/*
30 * In case MBEDTLS_PSA_CRYPTO_SPM is defined the code is built for SPM (Secure
31 * Partition Manager) integration which separate the code into two parts
32 * NSPE (Non-Secure Process Environment) and SPE (Secure Process Environment).
33 * In this mode an additional header file should be included.
34 */
mohammad160327010052018-07-03 13:16:15 +030035#if defined(MBEDTLS_PSA_CRYPTO_SPM)
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +030036/*
37 * PSA_CRYPTO_SECURE means that this file is compiled to the SPE side.
38 * some headers will be affected by this flag.
39 */
mohammad160327010052018-07-03 13:16:15 +030040#define PSA_CRYPTO_SECURE 1
41#include "crypto_spe.h"
42#endif
43
Gilles Peskinee59236f2018-01-27 23:32:46 +010044#include "psa/crypto.h"
45
Gilles Peskine5e769522018-11-20 21:59:56 +010046#include "psa_crypto_invasive.h"
Gilles Peskine961849f2018-11-30 18:54:54 +010047#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010048/* Include internal declarations that are useful for implementing persistently
49 * stored keys. */
50#include "psa_crypto_storage.h"
51
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010052#include <stdlib.h>
53#include <string.h>
54#if defined(MBEDTLS_PLATFORM_C)
55#include "mbedtls/platform.h"
56#else
57#define mbedtls_calloc calloc
58#define mbedtls_free free
59#endif
60
Gilles Peskinea5905292018-02-07 20:59:33 +010061#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020062#include "mbedtls/asn1.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020063#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/blowfish.h"
65#include "mbedtls/camellia.h"
66#include "mbedtls/cipher.h"
67#include "mbedtls/ccm.h"
68#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010069#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010070#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020071#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010072#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010073#include "mbedtls/entropy.h"
Netanel Gonen2bcd3122018-11-19 11:53:02 +020074#include "mbedtls/entropy_poll.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010075#include "mbedtls/error.h"
76#include "mbedtls/gcm.h"
77#include "mbedtls/md2.h"
78#include "mbedtls/md4.h"
79#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010080#include "mbedtls/md.h"
81#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010082#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010083#include "mbedtls/pk_internal.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010084#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010085#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010086#include "mbedtls/sha1.h"
87#include "mbedtls/sha256.h"
88#include "mbedtls/sha512.h"
89#include "mbedtls/xtea.h"
90
Netanel Gonen2bcd3122018-11-19 11:53:02 +020091#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
92#include "psa_prot_internal_storage.h"
93#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +010094
Gilles Peskine996deb12018-08-01 15:45:45 +020095#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
96
Gilles Peskinee59236f2018-01-27 23:32:46 +010097/* Implementation that should never be optimized out by the compiler */
98static void mbedtls_zeroize( void *v, size_t n )
99{
100 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
101}
102
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100103/* constant-time buffer comparison */
104static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
105{
106 size_t i;
107 unsigned char diff = 0;
108
109 for( i = 0; i < n; i++ )
110 diff |= a[i] ^ b[i];
111
112 return( diff );
113}
114
115
116
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100117/****************************************************************/
118/* Global data, support functions and library management */
119/****************************************************************/
120
Gilles Peskine2d277862018-06-18 15:41:12 +0200121typedef struct
122{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100123 psa_key_type_t type;
mohammad16038cc1cee2018-03-28 01:21:33 +0300124 psa_key_policy_t policy;
mohammad1603804cd712018-03-20 22:44:08 +0200125 psa_key_lifetime_t lifetime;
Gilles Peskine69f976b2018-11-30 18:46:56 +0100126 psa_key_id_t persistent_storage_id;
Gilles Peskine961849f2018-11-30 18:54:54 +0100127 unsigned allocated : 1;
Gilles Peskine2d277862018-06-18 15:41:12 +0200128 union
129 {
130 struct raw_data
131 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100132 uint8_t *data;
133 size_t bytes;
134 } raw;
Gilles Peskine969ac722018-01-28 18:16:59 +0100135#if defined(MBEDTLS_RSA_C)
136 mbedtls_rsa_context *rsa;
137#endif /* MBEDTLS_RSA_C */
138#if defined(MBEDTLS_ECP_C)
139 mbedtls_ecp_keypair *ecp;
140#endif /* MBEDTLS_ECP_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100141 } data;
142} key_slot_t;
143
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200144static int key_type_is_raw_bytes( psa_key_type_t type )
145{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200146 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200147}
148
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100149/* Values for psa_global_data_t::rng_state */
150#define RNG_NOT_INITIALIZED 0
151#define RNG_INITIALIZED 1
152#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100153
Gilles Peskine2d277862018-06-18 15:41:12 +0200154typedef struct
155{
Gilles Peskine5e769522018-11-20 21:59:56 +0100156 void (* entropy_init )( mbedtls_entropy_context *ctx );
157 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100158 mbedtls_entropy_context entropy;
159 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskine828ed142018-06-18 23:25:51 +0200160 key_slot_t key_slots[PSA_KEY_SLOT_COUNT];
Gilles Peskinec6b69072018-11-20 21:42:52 +0100161 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100162 unsigned rng_state : 2;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100163 unsigned key_slots_initialized : 1;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100164} psa_global_data_t;
165
166static psa_global_data_t global_data;
167
itayzafrir0adf0fc2018-09-06 16:24:41 +0300168#define GUARD_MODULE_INITIALIZED \
169 if( global_data.initialized == 0 ) \
170 return( PSA_ERROR_BAD_STATE );
171
Gilles Peskinee59236f2018-01-27 23:32:46 +0100172static psa_status_t mbedtls_to_psa_error( int ret )
173{
Gilles Peskinea5905292018-02-07 20:59:33 +0100174 /* If there's both a high-level code and low-level code, dispatch on
175 * the high-level code. */
176 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100177 {
178 case 0:
179 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100180
181 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
182 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
183 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
184 return( PSA_ERROR_NOT_SUPPORTED );
185 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
186 return( PSA_ERROR_HARDWARE_FAILURE );
187
188 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
189 return( PSA_ERROR_HARDWARE_FAILURE );
190
Gilles Peskine9a944802018-06-21 09:35:35 +0200191 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
192 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
193 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
194 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
195 case MBEDTLS_ERR_ASN1_INVALID_DATA:
196 return( PSA_ERROR_INVALID_ARGUMENT );
197 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
198 return( PSA_ERROR_INSUFFICIENT_MEMORY );
199 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
200 return( PSA_ERROR_BUFFER_TOO_SMALL );
201
Gilles Peskinea5905292018-02-07 20:59:33 +0100202 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
203 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
204 return( PSA_ERROR_NOT_SUPPORTED );
205 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
206 return( PSA_ERROR_HARDWARE_FAILURE );
207
208 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
209 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
210 return( PSA_ERROR_NOT_SUPPORTED );
211 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
212 return( PSA_ERROR_HARDWARE_FAILURE );
213
214 case MBEDTLS_ERR_CCM_BAD_INPUT:
215 return( PSA_ERROR_INVALID_ARGUMENT );
216 case MBEDTLS_ERR_CCM_AUTH_FAILED:
217 return( PSA_ERROR_INVALID_SIGNATURE );
218 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
219 return( PSA_ERROR_HARDWARE_FAILURE );
220
221 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
222 return( PSA_ERROR_NOT_SUPPORTED );
223 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
224 return( PSA_ERROR_INVALID_ARGUMENT );
225 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
226 return( PSA_ERROR_INSUFFICIENT_MEMORY );
227 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
228 return( PSA_ERROR_INVALID_PADDING );
229 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
230 return( PSA_ERROR_BAD_STATE );
231 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
232 return( PSA_ERROR_INVALID_SIGNATURE );
233 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
234 return( PSA_ERROR_TAMPERING_DETECTED );
235 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
236 return( PSA_ERROR_HARDWARE_FAILURE );
237
238 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
239 return( PSA_ERROR_HARDWARE_FAILURE );
240
241 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
242 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
243 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
244 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
245 return( PSA_ERROR_NOT_SUPPORTED );
246 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
247 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
248
249 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
250 return( PSA_ERROR_NOT_SUPPORTED );
251 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
252 return( PSA_ERROR_HARDWARE_FAILURE );
253
Gilles Peskinee59236f2018-01-27 23:32:46 +0100254 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
255 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
256 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
257 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100258
259 case MBEDTLS_ERR_GCM_AUTH_FAILED:
260 return( PSA_ERROR_INVALID_SIGNATURE );
261 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200262 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100263 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
264 return( PSA_ERROR_HARDWARE_FAILURE );
265
266 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
267 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
268 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
269 return( PSA_ERROR_HARDWARE_FAILURE );
270
271 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
272 return( PSA_ERROR_NOT_SUPPORTED );
273 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
274 return( PSA_ERROR_INVALID_ARGUMENT );
275 case MBEDTLS_ERR_MD_ALLOC_FAILED:
276 return( PSA_ERROR_INSUFFICIENT_MEMORY );
277 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
278 return( PSA_ERROR_STORAGE_FAILURE );
279 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
280 return( PSA_ERROR_HARDWARE_FAILURE );
281
Gilles Peskinef76aa772018-10-29 19:24:33 +0100282 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
283 return( PSA_ERROR_STORAGE_FAILURE );
284 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
285 return( PSA_ERROR_INVALID_ARGUMENT );
286 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
287 return( PSA_ERROR_INVALID_ARGUMENT );
288 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
289 return( PSA_ERROR_BUFFER_TOO_SMALL );
290 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
291 return( PSA_ERROR_INVALID_ARGUMENT );
292 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
293 return( PSA_ERROR_INVALID_ARGUMENT );
294 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
295 return( PSA_ERROR_INVALID_ARGUMENT );
296 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
297 return( PSA_ERROR_INSUFFICIENT_MEMORY );
298
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100299 case MBEDTLS_ERR_PK_ALLOC_FAILED:
300 return( PSA_ERROR_INSUFFICIENT_MEMORY );
301 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
302 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
303 return( PSA_ERROR_INVALID_ARGUMENT );
304 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100305 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100306 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
307 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
308 return( PSA_ERROR_INVALID_ARGUMENT );
309 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
310 return( PSA_ERROR_NOT_SUPPORTED );
311 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
312 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
313 return( PSA_ERROR_NOT_PERMITTED );
314 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
315 return( PSA_ERROR_INVALID_ARGUMENT );
316 case MBEDTLS_ERR_PK_INVALID_ALG:
317 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
318 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
319 return( PSA_ERROR_NOT_SUPPORTED );
320 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
321 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100322 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
323 return( PSA_ERROR_HARDWARE_FAILURE );
324
325 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
326 return( PSA_ERROR_HARDWARE_FAILURE );
327
328 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
329 return( PSA_ERROR_INVALID_ARGUMENT );
330 case MBEDTLS_ERR_RSA_INVALID_PADDING:
331 return( PSA_ERROR_INVALID_PADDING );
332 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
333 return( PSA_ERROR_HARDWARE_FAILURE );
334 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
335 return( PSA_ERROR_INVALID_ARGUMENT );
336 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
337 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
338 return( PSA_ERROR_TAMPERING_DETECTED );
339 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
340 return( PSA_ERROR_INVALID_SIGNATURE );
341 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
342 return( PSA_ERROR_BUFFER_TOO_SMALL );
343 case MBEDTLS_ERR_RSA_RNG_FAILED:
344 return( PSA_ERROR_INSUFFICIENT_MEMORY );
345 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
346 return( PSA_ERROR_NOT_SUPPORTED );
347 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
348 return( PSA_ERROR_HARDWARE_FAILURE );
349
350 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
351 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
352 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
353 return( PSA_ERROR_HARDWARE_FAILURE );
354
355 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
356 return( PSA_ERROR_INVALID_ARGUMENT );
357 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
358 return( PSA_ERROR_HARDWARE_FAILURE );
359
itayzafrir5c753392018-05-08 11:18:38 +0300360 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300361 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300362 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300363 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
364 return( PSA_ERROR_BUFFER_TOO_SMALL );
365 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
366 return( PSA_ERROR_NOT_SUPPORTED );
367 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
368 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
369 return( PSA_ERROR_INVALID_SIGNATURE );
370 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
371 return( PSA_ERROR_INSUFFICIENT_MEMORY );
372 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
373 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300374
Gilles Peskinee59236f2018-01-27 23:32:46 +0100375 default:
376 return( PSA_ERROR_UNKNOWN_ERROR );
377 }
378}
379
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200380
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200381
382
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100383/****************************************************************/
384/* Key management */
385/****************************************************************/
386
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100387#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200388static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
389{
390 switch( grpid )
391 {
392 case MBEDTLS_ECP_DP_SECP192R1:
393 return( PSA_ECC_CURVE_SECP192R1 );
394 case MBEDTLS_ECP_DP_SECP224R1:
395 return( PSA_ECC_CURVE_SECP224R1 );
396 case MBEDTLS_ECP_DP_SECP256R1:
397 return( PSA_ECC_CURVE_SECP256R1 );
398 case MBEDTLS_ECP_DP_SECP384R1:
399 return( PSA_ECC_CURVE_SECP384R1 );
400 case MBEDTLS_ECP_DP_SECP521R1:
401 return( PSA_ECC_CURVE_SECP521R1 );
402 case MBEDTLS_ECP_DP_BP256R1:
403 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
404 case MBEDTLS_ECP_DP_BP384R1:
405 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
406 case MBEDTLS_ECP_DP_BP512R1:
407 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
408 case MBEDTLS_ECP_DP_CURVE25519:
409 return( PSA_ECC_CURVE_CURVE25519 );
410 case MBEDTLS_ECP_DP_SECP192K1:
411 return( PSA_ECC_CURVE_SECP192K1 );
412 case MBEDTLS_ECP_DP_SECP224K1:
413 return( PSA_ECC_CURVE_SECP224K1 );
414 case MBEDTLS_ECP_DP_SECP256K1:
415 return( PSA_ECC_CURVE_SECP256K1 );
416 case MBEDTLS_ECP_DP_CURVE448:
417 return( PSA_ECC_CURVE_CURVE448 );
418 default:
419 return( 0 );
420 }
421}
422
Gilles Peskine12313cd2018-06-20 00:20:32 +0200423static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
424{
425 switch( curve )
426 {
427 case PSA_ECC_CURVE_SECP192R1:
428 return( MBEDTLS_ECP_DP_SECP192R1 );
429 case PSA_ECC_CURVE_SECP224R1:
430 return( MBEDTLS_ECP_DP_SECP224R1 );
431 case PSA_ECC_CURVE_SECP256R1:
432 return( MBEDTLS_ECP_DP_SECP256R1 );
433 case PSA_ECC_CURVE_SECP384R1:
434 return( MBEDTLS_ECP_DP_SECP384R1 );
435 case PSA_ECC_CURVE_SECP521R1:
436 return( MBEDTLS_ECP_DP_SECP521R1 );
437 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
438 return( MBEDTLS_ECP_DP_BP256R1 );
439 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
440 return( MBEDTLS_ECP_DP_BP384R1 );
441 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
442 return( MBEDTLS_ECP_DP_BP512R1 );
443 case PSA_ECC_CURVE_CURVE25519:
444 return( MBEDTLS_ECP_DP_CURVE25519 );
445 case PSA_ECC_CURVE_SECP192K1:
446 return( MBEDTLS_ECP_DP_SECP192K1 );
447 case PSA_ECC_CURVE_SECP224K1:
448 return( MBEDTLS_ECP_DP_SECP224K1 );
449 case PSA_ECC_CURVE_SECP256K1:
450 return( MBEDTLS_ECP_DP_SECP256K1 );
451 case PSA_ECC_CURVE_CURVE448:
452 return( MBEDTLS_ECP_DP_CURVE448 );
453 default:
454 return( MBEDTLS_ECP_DP_NONE );
455 }
456}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100457#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200458
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200459static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
460 size_t bits,
461 struct raw_data *raw )
462{
463 /* Check that the bit size is acceptable for the key type */
464 switch( type )
465 {
466 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200467 if( bits == 0 )
468 {
469 raw->bytes = 0;
470 raw->data = NULL;
471 return( PSA_SUCCESS );
472 }
473 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200474#if defined(MBEDTLS_MD_C)
475 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200476#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200477 case PSA_KEY_TYPE_DERIVE:
478 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200479#if defined(MBEDTLS_AES_C)
480 case PSA_KEY_TYPE_AES:
481 if( bits != 128 && bits != 192 && bits != 256 )
482 return( PSA_ERROR_INVALID_ARGUMENT );
483 break;
484#endif
485#if defined(MBEDTLS_CAMELLIA_C)
486 case PSA_KEY_TYPE_CAMELLIA:
487 if( bits != 128 && bits != 192 && bits != 256 )
488 return( PSA_ERROR_INVALID_ARGUMENT );
489 break;
490#endif
491#if defined(MBEDTLS_DES_C)
492 case PSA_KEY_TYPE_DES:
493 if( bits != 64 && bits != 128 && bits != 192 )
494 return( PSA_ERROR_INVALID_ARGUMENT );
495 break;
496#endif
497#if defined(MBEDTLS_ARC4_C)
498 case PSA_KEY_TYPE_ARC4:
499 if( bits < 8 || bits > 2048 )
500 return( PSA_ERROR_INVALID_ARGUMENT );
501 break;
502#endif
503 default:
504 return( PSA_ERROR_NOT_SUPPORTED );
505 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200506 if( bits % 8 != 0 )
507 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200508
509 /* Allocate memory for the key */
510 raw->bytes = PSA_BITS_TO_BYTES( bits );
511 raw->data = mbedtls_calloc( 1, raw->bytes );
512 if( raw->data == NULL )
513 {
514 raw->bytes = 0;
515 return( PSA_ERROR_INSUFFICIENT_MEMORY );
516 }
517 return( PSA_SUCCESS );
518}
519
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200520#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100521/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
522 * that are not a multiple of 8) well. For example, there is only
523 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
524 * way to return the exact bit size of a key.
525 * To keep things simple, reject non-byte-aligned key sizes. */
526static psa_status_t psa_check_rsa_key_byte_aligned(
527 const mbedtls_rsa_context *rsa )
528{
529 mbedtls_mpi n;
530 psa_status_t status;
531 mbedtls_mpi_init( &n );
532 status = mbedtls_to_psa_error(
533 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
534 if( status == PSA_SUCCESS )
535 {
536 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
537 status = PSA_ERROR_NOT_SUPPORTED;
538 }
539 mbedtls_mpi_free( &n );
540 return( status );
541}
542
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200543static psa_status_t psa_import_rsa_key( mbedtls_pk_context *pk,
544 mbedtls_rsa_context **p_rsa )
545{
546 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_RSA )
547 return( PSA_ERROR_INVALID_ARGUMENT );
548 else
549 {
550 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *pk );
Gilles Peskineaac64a22018-11-12 18:37:42 +0100551 /* The size of an RSA key doesn't have to be a multiple of 8.
552 * Mbed TLS supports non-byte-aligned key sizes, but not well.
553 * For example, mbedtls_rsa_get_len() returns the key size in
554 * bytes, not in bits. */
555 size_t bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
Gilles Peskine86a440b2018-11-12 18:39:40 +0100556 psa_status_t status;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200557 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
558 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +0100559 status = psa_check_rsa_key_byte_aligned( rsa );
560 if( status != PSA_SUCCESS )
561 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200562 *p_rsa = rsa;
563 return( PSA_SUCCESS );
564 }
565}
566#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
567
568#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinef76aa772018-10-29 19:24:33 +0100569/* Import an elliptic curve parsed by the mbedtls pk module. */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200570static psa_status_t psa_import_ecp_key( psa_ecc_curve_t expected_curve,
571 mbedtls_pk_context *pk,
572 mbedtls_ecp_keypair **p_ecp )
573{
574 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_ECKEY )
575 return( PSA_ERROR_INVALID_ARGUMENT );
576 else
577 {
578 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( *pk );
579 psa_ecc_curve_t actual_curve = mbedtls_ecc_group_to_psa( ecp->grp.id );
580 if( actual_curve != expected_curve )
581 return( PSA_ERROR_INVALID_ARGUMENT );
582 *p_ecp = ecp;
583 return( PSA_SUCCESS );
584 }
585}
586#endif /* defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C) */
587
Gilles Peskinef76aa772018-10-29 19:24:33 +0100588#if defined(MBEDTLS_ECP_C)
589/* Import a private key given as a byte string which is the private value
590 * in big-endian order. */
591static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
592 const uint8_t *data,
593 size_t data_length,
594 mbedtls_ecp_keypair **p_ecp )
595{
596 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
597 mbedtls_ecp_keypair *ecp = NULL;
598 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
599
600 *p_ecp = NULL;
601 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
602 if( ecp == NULL )
603 return( PSA_ERROR_INSUFFICIENT_MEMORY );
604
605 /* Load the group. */
606 status = mbedtls_to_psa_error(
607 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
608 if( status != PSA_SUCCESS )
609 goto exit;
610 /* Load the secret value. */
611 status = mbedtls_to_psa_error(
612 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
613 if( status != PSA_SUCCESS )
614 goto exit;
615 /* Validate the private key. */
616 status = mbedtls_to_psa_error(
617 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
618 if( status != PSA_SUCCESS )
619 goto exit;
620 /* Calculate the public key from the private key. */
621 status = mbedtls_to_psa_error(
622 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
623 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
624 if( status != PSA_SUCCESS )
625 goto exit;
626
627 *p_ecp = ecp;
628 return( PSA_SUCCESS );
629
630exit:
631 if( ecp != NULL )
632 {
633 mbedtls_ecp_keypair_free( ecp );
634 mbedtls_free( ecp );
635 }
636 return( status );
637}
638#endif /* defined(MBEDTLS_ECP_C) */
639
Darryl Green940d72c2018-07-13 13:18:51 +0100640static psa_status_t psa_import_key_into_slot( key_slot_t *slot,
641 const uint8_t *data,
642 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100643{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200644 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100645
Darryl Green940d72c2018-07-13 13:18:51 +0100646 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100647 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100648 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100649 if( data_length > SIZE_MAX / 8 )
650 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green940d72c2018-07-13 13:18:51 +0100651 status = prepare_raw_data_slot( slot->type,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200652 PSA_BYTES_TO_BITS( data_length ),
653 &slot->data.raw );
654 if( status != PSA_SUCCESS )
655 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200656 if( data_length != 0 )
657 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100658 }
659 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100660#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100661 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100662 {
Darryl Green940d72c2018-07-13 13:18:51 +0100663 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100664 data, data_length,
665 &slot->data.ecp );
666 if( status != PSA_SUCCESS )
667 return( status );
668 }
669 else
670#endif /* MBEDTLS_ECP_C */
Gilles Peskine969ac722018-01-28 18:16:59 +0100671#if defined(MBEDTLS_PK_PARSE_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100672 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
673 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100674 {
675 int ret;
Gilles Peskine969ac722018-01-28 18:16:59 +0100676 mbedtls_pk_context pk;
677 mbedtls_pk_init( &pk );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200678
679 /* Parse the data. */
Darryl Green940d72c2018-07-13 13:18:51 +0100680 if( PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100681 ret = mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 );
682 else
683 ret = mbedtls_pk_parse_public_key( &pk, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100684 if( ret != 0 )
685 return( mbedtls_to_psa_error( ret ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200686
687 /* We have something that the pkparse module recognizes.
688 * If it has the expected type and passes any type-specific
689 * checks, store it. */
Gilles Peskine969ac722018-01-28 18:16:59 +0100690#if defined(MBEDTLS_RSA_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100691 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200692 status = psa_import_rsa_key( &pk, &slot->data.rsa );
693 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100694#endif /* MBEDTLS_RSA_C */
695#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100696 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
697 status = psa_import_ecp_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200698 &pk, &slot->data.ecp );
699 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100700#endif /* MBEDTLS_ECP_C */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200701 {
702 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinec648d692018-06-28 08:46:13 +0200703 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200704
Gilles Peskinec648d692018-06-28 08:46:13 +0200705 /* Free the content of the pk object only on error. On success,
706 * the content of the object has been stored in the slot. */
707 if( status != PSA_SUCCESS )
708 {
709 mbedtls_pk_free( &pk );
710 return( status );
Gilles Peskine969ac722018-01-28 18:16:59 +0100711 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100712 }
713 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100714#endif /* defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100715 {
716 return( PSA_ERROR_NOT_SUPPORTED );
717 }
Darryl Green940d72c2018-07-13 13:18:51 +0100718 return( PSA_SUCCESS );
719}
720
Darryl Greend49a4992018-06-18 17:27:26 +0100721#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine69f976b2018-11-30 18:46:56 +0100722static psa_status_t psa_load_persistent_key_into_slot( key_slot_t *p_slot )
Darryl Greend49a4992018-06-18 17:27:26 +0100723{
724 psa_status_t status = PSA_SUCCESS;
725 uint8_t *key_data = NULL;
726 size_t key_data_length = 0;
727
Gilles Peskine69f976b2018-11-30 18:46:56 +0100728 status = psa_load_persistent_key( p_slot->persistent_storage_id,
729 &( p_slot )->type,
Darryl Greend49a4992018-06-18 17:27:26 +0100730 &( p_slot )->policy, &key_data,
731 &key_data_length );
732 if( status != PSA_SUCCESS )
733 goto exit;
734 status = psa_import_key_into_slot( p_slot,
735 key_data, key_data_length );
736exit:
737 psa_free_persistent_key_data( key_data, key_data_length );
738 return( status );
739}
740#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
741
Darryl Green06fd18d2018-07-16 11:21:11 +0100742/* Retrieve a key slot, occupied or not. */
Gilles Peskine961849f2018-11-30 18:54:54 +0100743static psa_status_t psa_get_key_slot( psa_key_slot_t key_or_handle,
Darryl Green06fd18d2018-07-16 11:21:11 +0100744 key_slot_t **p_slot )
745{
Gilles Peskine961849f2018-11-30 18:54:54 +0100746 psa_key_slot_t key = key_or_handle & ~PSA_KEY_HANDLE_ALLOCATED_FLAG;
747 int is_handle = ( key_or_handle & PSA_KEY_HANDLE_ALLOCATED_FLAG ) != 0;
748 psa_status_t error_if_invalid =
749 ( is_handle ?
750 PSA_ERROR_INVALID_HANDLE :
751 PSA_ERROR_INVALID_ARGUMENT );
752
Darryl Green06fd18d2018-07-16 11:21:11 +0100753 GUARD_MODULE_INITIALIZED;
754
755 /* 0 is not a valid slot number under any circumstance. This
756 * implementation provides slots number 1 to N where N is the
757 * number of available slots. */
758 if( key == 0 || key > ARRAY_LENGTH( global_data.key_slots ) )
Gilles Peskine961849f2018-11-30 18:54:54 +0100759 return( error_if_invalid );
Darryl Green06fd18d2018-07-16 11:21:11 +0100760
761 *p_slot = &global_data.key_slots[key - 1];
Darryl Greend49a4992018-06-18 17:27:26 +0100762
Gilles Peskine961849f2018-11-30 18:54:54 +0100763 /* Allocated slots must only be accessed via a handle.
764 * Unallocated slots must only be accessed directly. */
765 if( ( *p_slot )->allocated != is_handle )
766 return( error_if_invalid );
767
Darryl Greend49a4992018-06-18 17:27:26 +0100768#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine961849f2018-11-30 18:54:54 +0100769 if( ! ( *p_slot )->allocated &&
770 ( *p_slot )->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
Darryl Greend49a4992018-06-18 17:27:26 +0100771 {
772 /* There are two circumstances this can occur: the key material has
773 * not yet been created, or the key exists in storage but has not yet
774 * been loaded into memory. */
775 if( ( *p_slot )->type == PSA_KEY_TYPE_NONE )
776 {
777 psa_status_t status = PSA_SUCCESS;
Gilles Peskine69f976b2018-11-30 18:46:56 +0100778 status = psa_load_persistent_key_into_slot( *p_slot );
Darryl Greend49a4992018-06-18 17:27:26 +0100779 if( status != PSA_ERROR_EMPTY_SLOT )
780 return( status );
781 }
782 }
783#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
784
Darryl Green06fd18d2018-07-16 11:21:11 +0100785 return( PSA_SUCCESS );
786}
787
788/* Retrieve an empty key slot (slot with no key data, but possibly
789 * with some metadata such as a policy). */
790static psa_status_t psa_get_empty_key_slot( psa_key_slot_t key,
791 key_slot_t **p_slot )
792{
793 psa_status_t status;
794 key_slot_t *slot = NULL;
795
796 *p_slot = NULL;
797
798 status = psa_get_key_slot( key, &slot );
799 if( status != PSA_SUCCESS )
800 return( status );
801
802 if( slot->type != PSA_KEY_TYPE_NONE )
803 return( PSA_ERROR_OCCUPIED_SLOT );
804
805 *p_slot = slot;
806 return( status );
807}
808
809/** Retrieve a slot which must contain a key. The key must have allow all the
810 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
811 * operations with this algorithm. */
812static psa_status_t psa_get_key_from_slot( psa_key_slot_t key,
813 key_slot_t **p_slot,
814 psa_key_usage_t usage,
815 psa_algorithm_t alg )
816{
817 psa_status_t status;
818 key_slot_t *slot = NULL;
819
820 *p_slot = NULL;
821
822 status = psa_get_key_slot( key, &slot );
823 if( status != PSA_SUCCESS )
824 return( status );
825 if( slot->type == PSA_KEY_TYPE_NONE )
826 return( PSA_ERROR_EMPTY_SLOT );
827
828 /* Enforce that usage policy for the key slot contains all the flags
829 * required by the usage parameter. There is one exception: public
830 * keys can always be exported, so we treat public key objects as
831 * if they had the export flag. */
832 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
833 usage &= ~PSA_KEY_USAGE_EXPORT;
834 if( ( slot->policy.usage & usage ) != usage )
835 return( PSA_ERROR_NOT_PERMITTED );
836 if( alg != 0 && ( alg != slot->policy.alg ) )
837 return( PSA_ERROR_NOT_PERMITTED );
838
839 *p_slot = slot;
840 return( PSA_SUCCESS );
841}
Darryl Green940d72c2018-07-13 13:18:51 +0100842
Darryl Green40225ba2018-11-15 14:48:15 +0000843static psa_status_t psa_remove_key_data_from_memory( key_slot_t *slot )
844{
845 if( slot->type == PSA_KEY_TYPE_NONE )
846 {
847 /* No key material to clean. */
848 }
849 else if( key_type_is_raw_bytes( slot->type ) )
850 {
851 mbedtls_free( slot->data.raw.data );
852 }
853 else
854#if defined(MBEDTLS_RSA_C)
855 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
856 {
857 mbedtls_rsa_free( slot->data.rsa );
858 mbedtls_free( slot->data.rsa );
859 }
860 else
861#endif /* defined(MBEDTLS_RSA_C) */
862#if defined(MBEDTLS_ECP_C)
863 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
864 {
865 mbedtls_ecp_keypair_free( slot->data.ecp );
866 mbedtls_free( slot->data.ecp );
867 }
868 else
869#endif /* defined(MBEDTLS_ECP_C) */
870 {
871 /* Shouldn't happen: the key type is not any type that we
872 * put in. */
873 return( PSA_ERROR_TAMPERING_DETECTED );
874 }
875
876 return( PSA_SUCCESS );
877}
878
Gilles Peskine961849f2018-11-30 18:54:54 +0100879/* A slot is available if nothing has been set in it: default lifetime
880 * and policy, no key type. */
881static int psa_internal_is_slot_available( key_slot_t *slot )
882{
883 if( slot->allocated )
884 return( 0 );
885 if( slot->type != PSA_KEY_TYPE_NONE )
886 return( 0 );
887 if( slot->lifetime != PSA_KEY_LIFETIME_VOLATILE )
888 return( 0 );
889 if( slot->policy.usage != 0 || slot->policy.alg != 0 )
890 return( 0 );
891 return( 1 );
892}
893
894psa_status_t psa_internal_allocate_key_slot( psa_key_handle_t *handle )
895{
896 psa_key_slot_t key;
897 for( key = PSA_KEY_SLOT_COUNT; key != 0; --( key ) )
898 {
899 key_slot_t *slot = &global_data.key_slots[key - 1];
900 if( psa_internal_is_slot_available( slot ) )
901 {
902 slot->allocated = 1;
903 *handle = key | PSA_KEY_HANDLE_ALLOCATED_FLAG;
904 return( PSA_SUCCESS );
905 }
906 }
907 return( PSA_ERROR_INSUFFICIENT_MEMORY );
908}
909
910psa_status_t psa_internal_make_key_persistent( psa_key_handle_t handle,
911 psa_key_id_t id )
912{
913 key_slot_t *slot;
914 psa_status_t status;
915
916 /* Reject id=0 because by general library conventions, 0 is an invalid
917 * value wherever possible. */
918 if( id == 0 )
919 return( PSA_ERROR_INVALID_ARGUMENT );
920 /* Reject high values because the file names are reserved for the
921 * library's internal use. */
922 if( id >= 0xffff0000 )
923 return( PSA_ERROR_INVALID_ARGUMENT );
924 /* Reject values that don't fit in the key slot number type.
925 * This is a temporary limitation due to the library's internal
926 * plumbing. */
927 if( id > (psa_key_slot_t)( -1 ) )
928 return( PSA_ERROR_INVALID_ARGUMENT );
929
930 status = psa_get_key_slot( handle, &slot );
931 if( status != PSA_SUCCESS )
932 return( status );
933
934 slot->lifetime = PSA_KEY_LIFETIME_PERSISTENT;
935 slot->persistent_storage_id = id;
936 status = psa_load_persistent_key_into_slot( slot );
937
938 return( status );
939}
940
941psa_status_t psa_internal_release_key_slot( psa_key_handle_t handle )
942{
943 psa_key_slot_t key;
944 key_slot_t *slot;
945 psa_status_t status;
946 /* Don't call psa_get_key_slot() so as not to trigger its automatic
947 * loading of persistent key data. */
948 if( ( handle & PSA_KEY_HANDLE_ALLOCATED_FLAG ) == 0 )
949 return( PSA_ERROR_INVALID_HANDLE );
950 key = handle & ~PSA_KEY_HANDLE_ALLOCATED_FLAG;
951 if( key == 0 || key > ARRAY_LENGTH( global_data.key_slots ) )
952 return( PSA_ERROR_INVALID_HANDLE );
953 slot = &global_data.key_slots[key - 1];
954 if( ! slot->allocated )
955 return( PSA_ERROR_INVALID_HANDLE );
956 status = psa_remove_key_data_from_memory( slot );
957 memset( slot, 0, sizeof( *slot ) );
958 return( status );
959}
960
Darryl Green940d72c2018-07-13 13:18:51 +0100961psa_status_t psa_import_key( psa_key_slot_t key,
962 psa_key_type_t type,
963 const uint8_t *data,
964 size_t data_length )
965{
966 key_slot_t *slot;
967 psa_status_t status;
968
969 status = psa_get_empty_key_slot( key, &slot );
970 if( status != PSA_SUCCESS )
971 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100972
973 slot->type = type;
Darryl Green940d72c2018-07-13 13:18:51 +0100974
975 status = psa_import_key_into_slot( slot, data, data_length );
976 if( status != PSA_SUCCESS )
977 {
978 slot->type = PSA_KEY_TYPE_NONE;
979 return( status );
980 }
981
Darryl Greend49a4992018-06-18 17:27:26 +0100982#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
983 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
984 {
985 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +0100986 status = psa_save_persistent_key( slot->persistent_storage_id,
987 slot->type, &slot->policy, data,
Darryl Greend49a4992018-06-18 17:27:26 +0100988 data_length );
989 if( status != PSA_SUCCESS )
990 {
991 (void) psa_remove_key_data_from_memory( slot );
992 slot->type = PSA_KEY_TYPE_NONE;
993 }
994 }
995#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
996
997 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100998}
999
Gilles Peskine2d277862018-06-18 15:41:12 +02001000psa_status_t psa_destroy_key( psa_key_slot_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001001{
1002 key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +01001003 psa_status_t status = PSA_SUCCESS;
1004 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001005
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001006 status = psa_get_key_slot( key, &slot );
1007 if( status != PSA_SUCCESS )
1008 return( status );
Darryl Greend49a4992018-06-18 17:27:26 +01001009#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
1010 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
1011 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01001012 storage_status =
1013 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +01001014 }
1015#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1016 status = psa_remove_key_data_from_memory( slot );
1017 /* Zeroize the slot to wipe metadata such as policies. */
1018 mbedtls_zeroize( slot, sizeof( *slot ) );
1019 if( status != PSA_SUCCESS )
1020 return( status );
1021 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001022}
1023
Gilles Peskineb870b182018-07-06 16:02:09 +02001024/* Return the size of the key in the given slot, in bits. */
1025static size_t psa_get_key_bits( const key_slot_t *slot )
1026{
1027 if( key_type_is_raw_bytes( slot->type ) )
1028 return( slot->data.raw.bytes * 8 );
1029#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001030 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +01001031 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001032#endif /* defined(MBEDTLS_RSA_C) */
1033#if defined(MBEDTLS_ECP_C)
1034 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1035 return( slot->data.ecp->grp.pbits );
1036#endif /* defined(MBEDTLS_ECP_C) */
1037 /* Shouldn't happen except on an empty slot. */
1038 return( 0 );
1039}
1040
Gilles Peskine2d277862018-06-18 15:41:12 +02001041psa_status_t psa_get_key_information( psa_key_slot_t key,
1042 psa_key_type_t *type,
1043 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001044{
1045 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001046 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001047
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001048 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001049 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001050 if( bits != NULL )
1051 *bits = 0;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001052 status = psa_get_key_slot( key, &slot );
1053 if( status != PSA_SUCCESS )
1054 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001055
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001056 if( slot->type == PSA_KEY_TYPE_NONE )
1057 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001058 if( type != NULL )
1059 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +02001060 if( bits != NULL )
1061 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001062 return( PSA_SUCCESS );
1063}
1064
Darryl Greendd8fb772018-11-07 16:00:44 +00001065static psa_status_t psa_internal_export_key( key_slot_t *slot,
Gilles Peskine2d277862018-06-18 15:41:12 +02001066 uint8_t *data,
1067 size_t data_size,
1068 size_t *data_length,
1069 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001070{
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001071 *data_length = 0;
1072
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001073 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001074 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001075
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001076 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001077 {
1078 if( slot->data.raw.bytes > data_size )
1079 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001080 if( data_size != 0 )
1081 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001082 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001083 memset( data + slot->data.raw.bytes, 0,
1084 data_size - slot->data.raw.bytes );
1085 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001086 *data_length = slot->data.raw.bytes;
1087 return( PSA_SUCCESS );
1088 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001089#if defined(MBEDTLS_ECP_C)
1090 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
1091 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001092 psa_status_t status;
1093
Gilles Peskine188c71e2018-10-29 19:26:02 +01001094 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( slot ) );
1095 if( bytes > data_size )
1096 return( PSA_ERROR_BUFFER_TOO_SMALL );
1097 status = mbedtls_to_psa_error(
1098 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1099 if( status != PSA_SUCCESS )
1100 return( status );
1101 memset( data + bytes, 0, data_size - bytes );
1102 *data_length = bytes;
1103 return( PSA_SUCCESS );
1104 }
1105#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001106 else
Moran Peker17e36e12018-05-02 12:55:20 +03001107 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001108#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001109 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001110 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001111 {
Moran Pekera998bc62018-04-16 18:16:20 +03001112 mbedtls_pk_context pk;
1113 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001114 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001115 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001116#if defined(MBEDTLS_RSA_C)
1117 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001118 pk.pk_info = &mbedtls_rsa_info;
1119 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001120#else
1121 return( PSA_ERROR_NOT_SUPPORTED );
1122#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001123 }
1124 else
1125 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001126#if defined(MBEDTLS_ECP_C)
1127 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001128 pk.pk_info = &mbedtls_eckey_info;
1129 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001130#else
1131 return( PSA_ERROR_NOT_SUPPORTED );
1132#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001133 }
Moran Pekerd7326592018-05-29 16:56:39 +03001134 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001135 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +03001136 else
1137 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +03001138 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001139 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001140 /* If data_size is 0 then data may be NULL and then the
1141 * call to memset would have undefined behavior. */
1142 if( data_size != 0 )
1143 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001144 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001145 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001146 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1147 * Move the data to the beginning and erase remaining data
1148 * at the original location. */
1149 if( 2 * (size_t) ret <= data_size )
1150 {
1151 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001152 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001153 }
1154 else if( (size_t) ret < data_size )
1155 {
1156 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001157 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001158 }
Moran Pekera998bc62018-04-16 18:16:20 +03001159 *data_length = ret;
1160 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001161 }
1162 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001163#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001164 {
1165 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001166 it is valid for a special-purpose implementation to omit
1167 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001168 return( PSA_ERROR_NOT_SUPPORTED );
1169 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001170 }
1171}
1172
Gilles Peskine2d277862018-06-18 15:41:12 +02001173psa_status_t psa_export_key( psa_key_slot_t key,
1174 uint8_t *data,
1175 size_t data_size,
1176 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001177{
Darryl Greendd8fb772018-11-07 16:00:44 +00001178 key_slot_t *slot;
1179 psa_status_t status;
1180
1181 /* Set the key to empty now, so that even when there are errors, we always
1182 * set data_length to a value between 0 and data_size. On error, setting
1183 * the key to empty is a good choice because an empty key representation is
1184 * unlikely to be accepted anywhere. */
1185 *data_length = 0;
1186
1187 /* Export requires the EXPORT flag. There is an exception for public keys,
1188 * which don't require any flag, but psa_get_key_from_slot takes
1189 * care of this. */
1190 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_EXPORT, 0 );
1191 if( status != PSA_SUCCESS )
1192 return( status );
1193 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001194 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001195}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001196
Gilles Peskine2d277862018-06-18 15:41:12 +02001197psa_status_t psa_export_public_key( psa_key_slot_t key,
1198 uint8_t *data,
1199 size_t data_size,
1200 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001201{
Darryl Greendd8fb772018-11-07 16:00:44 +00001202 key_slot_t *slot;
1203 psa_status_t status;
1204
1205 /* Set the key to empty now, so that even when there are errors, we always
1206 * set data_length to a value between 0 and data_size. On error, setting
1207 * the key to empty is a good choice because an empty key representation is
1208 * unlikely to be accepted anywhere. */
1209 *data_length = 0;
1210
1211 /* Exporting a public key doesn't require a usage flag. */
1212 status = psa_get_key_from_slot( key, &slot, 0, 0 );
1213 if( status != PSA_SUCCESS )
1214 return( status );
1215 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001216 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001217}
1218
Darryl Green0c6575a2018-11-07 16:05:30 +00001219#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine69f976b2018-11-30 18:46:56 +01001220static psa_status_t psa_save_generated_persistent_key( key_slot_t *slot,
Darryl Green0c6575a2018-11-07 16:05:30 +00001221 size_t bits )
1222{
1223 psa_status_t status;
1224 uint8_t *data;
1225 size_t key_length;
1226 size_t data_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type, bits );
1227 data = mbedtls_calloc( 1, data_size );
itayzafrir910c76b2018-11-21 16:03:21 +02001228 if( data == NULL )
1229 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Darryl Green0c6575a2018-11-07 16:05:30 +00001230 /* Get key data in export format */
1231 status = psa_internal_export_key( slot, data, data_size, &key_length, 0 );
1232 if( status != PSA_SUCCESS )
1233 {
1234 slot->type = PSA_KEY_TYPE_NONE;
1235 goto exit;
1236 }
1237 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +01001238 status = psa_save_persistent_key( slot->persistent_storage_id,
1239 slot->type, &slot->policy,
Darryl Green0c6575a2018-11-07 16:05:30 +00001240 data, key_length );
1241 if( status != PSA_SUCCESS )
1242 {
1243 slot->type = PSA_KEY_TYPE_NONE;
1244 }
1245exit:
1246 mbedtls_zeroize( data, key_length );
1247 mbedtls_free( data );
1248 return( status );
1249}
1250#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1251
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001252
1253
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001254/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001255/* Message digests */
1256/****************************************************************/
1257
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001258static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001259{
1260 switch( alg )
1261 {
1262#if defined(MBEDTLS_MD2_C)
1263 case PSA_ALG_MD2:
1264 return( &mbedtls_md2_info );
1265#endif
1266#if defined(MBEDTLS_MD4_C)
1267 case PSA_ALG_MD4:
1268 return( &mbedtls_md4_info );
1269#endif
1270#if defined(MBEDTLS_MD5_C)
1271 case PSA_ALG_MD5:
1272 return( &mbedtls_md5_info );
1273#endif
1274#if defined(MBEDTLS_RIPEMD160_C)
1275 case PSA_ALG_RIPEMD160:
1276 return( &mbedtls_ripemd160_info );
1277#endif
1278#if defined(MBEDTLS_SHA1_C)
1279 case PSA_ALG_SHA_1:
1280 return( &mbedtls_sha1_info );
1281#endif
1282#if defined(MBEDTLS_SHA256_C)
1283 case PSA_ALG_SHA_224:
1284 return( &mbedtls_sha224_info );
1285 case PSA_ALG_SHA_256:
1286 return( &mbedtls_sha256_info );
1287#endif
1288#if defined(MBEDTLS_SHA512_C)
1289 case PSA_ALG_SHA_384:
1290 return( &mbedtls_sha384_info );
1291 case PSA_ALG_SHA_512:
1292 return( &mbedtls_sha512_info );
1293#endif
1294 default:
1295 return( NULL );
1296 }
1297}
1298
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001299psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1300{
1301 switch( operation->alg )
1302 {
Gilles Peskine81736312018-06-26 15:04:31 +02001303 case 0:
1304 /* The object has (apparently) been initialized but it is not
1305 * in use. It's ok to call abort on such an object, and there's
1306 * nothing to do. */
1307 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001308#if defined(MBEDTLS_MD2_C)
1309 case PSA_ALG_MD2:
1310 mbedtls_md2_free( &operation->ctx.md2 );
1311 break;
1312#endif
1313#if defined(MBEDTLS_MD4_C)
1314 case PSA_ALG_MD4:
1315 mbedtls_md4_free( &operation->ctx.md4 );
1316 break;
1317#endif
1318#if defined(MBEDTLS_MD5_C)
1319 case PSA_ALG_MD5:
1320 mbedtls_md5_free( &operation->ctx.md5 );
1321 break;
1322#endif
1323#if defined(MBEDTLS_RIPEMD160_C)
1324 case PSA_ALG_RIPEMD160:
1325 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1326 break;
1327#endif
1328#if defined(MBEDTLS_SHA1_C)
1329 case PSA_ALG_SHA_1:
1330 mbedtls_sha1_free( &operation->ctx.sha1 );
1331 break;
1332#endif
1333#if defined(MBEDTLS_SHA256_C)
1334 case PSA_ALG_SHA_224:
1335 case PSA_ALG_SHA_256:
1336 mbedtls_sha256_free( &operation->ctx.sha256 );
1337 break;
1338#endif
1339#if defined(MBEDTLS_SHA512_C)
1340 case PSA_ALG_SHA_384:
1341 case PSA_ALG_SHA_512:
1342 mbedtls_sha512_free( &operation->ctx.sha512 );
1343 break;
1344#endif
1345 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001346 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001347 }
1348 operation->alg = 0;
1349 return( PSA_SUCCESS );
1350}
1351
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001352psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001353 psa_algorithm_t alg )
1354{
1355 int ret;
1356 operation->alg = 0;
1357 switch( alg )
1358 {
1359#if defined(MBEDTLS_MD2_C)
1360 case PSA_ALG_MD2:
1361 mbedtls_md2_init( &operation->ctx.md2 );
1362 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1363 break;
1364#endif
1365#if defined(MBEDTLS_MD4_C)
1366 case PSA_ALG_MD4:
1367 mbedtls_md4_init( &operation->ctx.md4 );
1368 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1369 break;
1370#endif
1371#if defined(MBEDTLS_MD5_C)
1372 case PSA_ALG_MD5:
1373 mbedtls_md5_init( &operation->ctx.md5 );
1374 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1375 break;
1376#endif
1377#if defined(MBEDTLS_RIPEMD160_C)
1378 case PSA_ALG_RIPEMD160:
1379 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1380 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1381 break;
1382#endif
1383#if defined(MBEDTLS_SHA1_C)
1384 case PSA_ALG_SHA_1:
1385 mbedtls_sha1_init( &operation->ctx.sha1 );
1386 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1387 break;
1388#endif
1389#if defined(MBEDTLS_SHA256_C)
1390 case PSA_ALG_SHA_224:
1391 mbedtls_sha256_init( &operation->ctx.sha256 );
1392 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1393 break;
1394 case PSA_ALG_SHA_256:
1395 mbedtls_sha256_init( &operation->ctx.sha256 );
1396 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1397 break;
1398#endif
1399#if defined(MBEDTLS_SHA512_C)
1400 case PSA_ALG_SHA_384:
1401 mbedtls_sha512_init( &operation->ctx.sha512 );
1402 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1403 break;
1404 case PSA_ALG_SHA_512:
1405 mbedtls_sha512_init( &operation->ctx.sha512 );
1406 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1407 break;
1408#endif
1409 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001410 return( PSA_ALG_IS_HASH( alg ) ?
1411 PSA_ERROR_NOT_SUPPORTED :
1412 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001413 }
1414 if( ret == 0 )
1415 operation->alg = alg;
1416 else
1417 psa_hash_abort( operation );
1418 return( mbedtls_to_psa_error( ret ) );
1419}
1420
1421psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1422 const uint8_t *input,
1423 size_t input_length )
1424{
1425 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001426
1427 /* Don't require hash implementations to behave correctly on a
1428 * zero-length input, which may have an invalid pointer. */
1429 if( input_length == 0 )
1430 return( PSA_SUCCESS );
1431
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001432 switch( operation->alg )
1433 {
1434#if defined(MBEDTLS_MD2_C)
1435 case PSA_ALG_MD2:
1436 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1437 input, input_length );
1438 break;
1439#endif
1440#if defined(MBEDTLS_MD4_C)
1441 case PSA_ALG_MD4:
1442 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1443 input, input_length );
1444 break;
1445#endif
1446#if defined(MBEDTLS_MD5_C)
1447 case PSA_ALG_MD5:
1448 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1449 input, input_length );
1450 break;
1451#endif
1452#if defined(MBEDTLS_RIPEMD160_C)
1453 case PSA_ALG_RIPEMD160:
1454 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1455 input, input_length );
1456 break;
1457#endif
1458#if defined(MBEDTLS_SHA1_C)
1459 case PSA_ALG_SHA_1:
1460 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1461 input, input_length );
1462 break;
1463#endif
1464#if defined(MBEDTLS_SHA256_C)
1465 case PSA_ALG_SHA_224:
1466 case PSA_ALG_SHA_256:
1467 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1468 input, input_length );
1469 break;
1470#endif
1471#if defined(MBEDTLS_SHA512_C)
1472 case PSA_ALG_SHA_384:
1473 case PSA_ALG_SHA_512:
1474 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1475 input, input_length );
1476 break;
1477#endif
1478 default:
1479 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1480 break;
1481 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001482
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001483 if( ret != 0 )
1484 psa_hash_abort( operation );
1485 return( mbedtls_to_psa_error( ret ) );
1486}
1487
1488psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1489 uint8_t *hash,
1490 size_t hash_size,
1491 size_t *hash_length )
1492{
itayzafrir40835d42018-08-02 13:14:17 +03001493 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001494 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001495 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001496
1497 /* Fill the output buffer with something that isn't a valid hash
1498 * (barring an attack on the hash and deliberately-crafted input),
1499 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001500 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001501 /* If hash_size is 0 then hash may be NULL and then the
1502 * call to memset would have undefined behavior. */
1503 if( hash_size != 0 )
1504 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001505
1506 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001507 {
1508 status = PSA_ERROR_BUFFER_TOO_SMALL;
1509 goto exit;
1510 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001511
1512 switch( operation->alg )
1513 {
1514#if defined(MBEDTLS_MD2_C)
1515 case PSA_ALG_MD2:
1516 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1517 break;
1518#endif
1519#if defined(MBEDTLS_MD4_C)
1520 case PSA_ALG_MD4:
1521 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1522 break;
1523#endif
1524#if defined(MBEDTLS_MD5_C)
1525 case PSA_ALG_MD5:
1526 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1527 break;
1528#endif
1529#if defined(MBEDTLS_RIPEMD160_C)
1530 case PSA_ALG_RIPEMD160:
1531 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1532 break;
1533#endif
1534#if defined(MBEDTLS_SHA1_C)
1535 case PSA_ALG_SHA_1:
1536 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1537 break;
1538#endif
1539#if defined(MBEDTLS_SHA256_C)
1540 case PSA_ALG_SHA_224:
1541 case PSA_ALG_SHA_256:
1542 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1543 break;
1544#endif
1545#if defined(MBEDTLS_SHA512_C)
1546 case PSA_ALG_SHA_384:
1547 case PSA_ALG_SHA_512:
1548 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1549 break;
1550#endif
1551 default:
1552 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1553 break;
1554 }
itayzafrir40835d42018-08-02 13:14:17 +03001555 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001556
itayzafrir40835d42018-08-02 13:14:17 +03001557exit:
1558 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001559 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001560 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001561 return( psa_hash_abort( operation ) );
1562 }
1563 else
1564 {
1565 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001566 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001567 }
1568}
1569
Gilles Peskine2d277862018-06-18 15:41:12 +02001570psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1571 const uint8_t *hash,
1572 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001573{
1574 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1575 size_t actual_hash_length;
1576 psa_status_t status = psa_hash_finish( operation,
1577 actual_hash, sizeof( actual_hash ),
1578 &actual_hash_length );
1579 if( status != PSA_SUCCESS )
1580 return( status );
1581 if( actual_hash_length != hash_length )
1582 return( PSA_ERROR_INVALID_SIGNATURE );
1583 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1584 return( PSA_ERROR_INVALID_SIGNATURE );
1585 return( PSA_SUCCESS );
1586}
1587
1588
1589
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001590/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001591/* MAC */
1592/****************************************************************/
1593
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001594static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001595 psa_algorithm_t alg,
1596 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001597 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001598 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001599{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001600 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001601 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001602
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001603 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001604 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001605
Gilles Peskine8c9def32018-02-08 10:02:12 +01001606 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1607 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001608 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001609 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001610 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001611 mode = MBEDTLS_MODE_STREAM;
1612 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001613 case PSA_ALG_CTR:
1614 mode = MBEDTLS_MODE_CTR;
1615 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001616 case PSA_ALG_CFB:
1617 mode = MBEDTLS_MODE_CFB;
1618 break;
1619 case PSA_ALG_OFB:
1620 mode = MBEDTLS_MODE_OFB;
1621 break;
1622 case PSA_ALG_CBC_NO_PADDING:
1623 mode = MBEDTLS_MODE_CBC;
1624 break;
1625 case PSA_ALG_CBC_PKCS7:
1626 mode = MBEDTLS_MODE_CBC;
1627 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001628 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001629 mode = MBEDTLS_MODE_CCM;
1630 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001631 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001632 mode = MBEDTLS_MODE_GCM;
1633 break;
1634 default:
1635 return( NULL );
1636 }
1637 }
1638 else if( alg == PSA_ALG_CMAC )
1639 mode = MBEDTLS_MODE_ECB;
1640 else if( alg == PSA_ALG_GMAC )
1641 mode = MBEDTLS_MODE_GCM;
1642 else
1643 return( NULL );
1644
1645 switch( key_type )
1646 {
1647 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001648 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001649 break;
1650 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001651 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1652 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001653 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001654 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001655 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001656 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001657 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1658 * but two-key Triple-DES is functionally three-key Triple-DES
1659 * with K1=K3, so that's how we present it to mbedtls. */
1660 if( key_bits == 128 )
1661 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001662 break;
1663 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001664 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001665 break;
1666 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001667 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001668 break;
1669 default:
1670 return( NULL );
1671 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001672 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001673 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001674
Jaeden Amero23bbb752018-06-26 14:16:54 +01001675 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1676 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001677}
1678
Gilles Peskinea05219c2018-11-16 16:02:56 +01001679#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001680static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001681{
Gilles Peskine2d277862018-06-18 15:41:12 +02001682 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001683 {
1684 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001685 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001686 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001687 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001688 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001689 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001690 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001691 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001692 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001693 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001694 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001695 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001696 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001697 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001698 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001699 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001700 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001701 return( 128 );
1702 default:
1703 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001704 }
1705}
Gilles Peskinea05219c2018-11-16 16:02:56 +01001706#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001707
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001708/* Initialize the MAC operation structure. Once this function has been
1709 * called, psa_mac_abort can run and will do the right thing. */
1710static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1711 psa_algorithm_t alg )
1712{
1713 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1714
1715 operation->alg = alg;
1716 operation->key_set = 0;
1717 operation->iv_set = 0;
1718 operation->iv_required = 0;
1719 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001720 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001721
1722#if defined(MBEDTLS_CMAC_C)
1723 if( alg == PSA_ALG_CMAC )
1724 {
1725 operation->iv_required = 0;
1726 mbedtls_cipher_init( &operation->ctx.cmac );
1727 status = PSA_SUCCESS;
1728 }
1729 else
1730#endif /* MBEDTLS_CMAC_C */
1731#if defined(MBEDTLS_MD_C)
1732 if( PSA_ALG_IS_HMAC( operation->alg ) )
1733 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001734 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1735 operation->ctx.hmac.hash_ctx.alg = 0;
1736 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001737 }
1738 else
1739#endif /* MBEDTLS_MD_C */
1740 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001741 if( ! PSA_ALG_IS_MAC( alg ) )
1742 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001743 }
1744
1745 if( status != PSA_SUCCESS )
1746 memset( operation, 0, sizeof( *operation ) );
1747 return( status );
1748}
1749
Gilles Peskine01126fa2018-07-12 17:04:55 +02001750#if defined(MBEDTLS_MD_C)
1751static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1752{
1753 mbedtls_zeroize( hmac->opad, sizeof( hmac->opad ) );
1754 return( psa_hash_abort( &hmac->hash_ctx ) );
1755}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01001756
1757static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
1758{
1759 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
1760 memset( hmac, 0, sizeof( *hmac ) );
1761}
Gilles Peskine01126fa2018-07-12 17:04:55 +02001762#endif /* MBEDTLS_MD_C */
1763
Gilles Peskine8c9def32018-02-08 10:02:12 +01001764psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1765{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001766 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001767 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001768 /* The object has (apparently) been initialized but it is not
1769 * in use. It's ok to call abort on such an object, and there's
1770 * nothing to do. */
1771 return( PSA_SUCCESS );
1772 }
1773 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001774#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001775 if( operation->alg == PSA_ALG_CMAC )
1776 {
1777 mbedtls_cipher_free( &operation->ctx.cmac );
1778 }
1779 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001780#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001781#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001782 if( PSA_ALG_IS_HMAC( operation->alg ) )
1783 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001784 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001785 }
1786 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001787#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001788 {
1789 /* Sanity check (shouldn't happen: operation->alg should
1790 * always have been initialized to a valid value). */
1791 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001792 }
Moran Peker41deec42018-04-04 15:43:05 +03001793
Gilles Peskine8c9def32018-02-08 10:02:12 +01001794 operation->alg = 0;
1795 operation->key_set = 0;
1796 operation->iv_set = 0;
1797 operation->iv_required = 0;
1798 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001799 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001800
Gilles Peskine8c9def32018-02-08 10:02:12 +01001801 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001802
1803bad_state:
1804 /* If abort is called on an uninitialized object, we can't trust
1805 * anything. Wipe the object in case it contains confidential data.
1806 * This may result in a memory leak if a pointer gets overwritten,
1807 * but it's too late to do anything about this. */
1808 memset( operation, 0, sizeof( *operation ) );
1809 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001810}
1811
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001812#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001813static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001814 size_t key_bits,
1815 key_slot_t *slot,
1816 const mbedtls_cipher_info_t *cipher_info )
1817{
1818 int ret;
1819
1820 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001821
1822 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1823 if( ret != 0 )
1824 return( ret );
1825
1826 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1827 slot->data.raw.data,
1828 key_bits );
1829 return( ret );
1830}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001831#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001832
Gilles Peskine248051a2018-06-20 16:09:38 +02001833#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001834static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1835 const uint8_t *key,
1836 size_t key_length,
1837 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001838{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001839 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001840 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001841 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001842 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001843 psa_status_t status;
1844
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001845 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1846 * overflow below. This should never trigger if the hash algorithm
1847 * is implemented correctly. */
1848 /* The size checks against the ipad and opad buffers cannot be written
1849 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1850 * because that triggers -Wlogical-op on GCC 7.3. */
1851 if( block_size > sizeof( ipad ) )
1852 return( PSA_ERROR_NOT_SUPPORTED );
1853 if( block_size > sizeof( hmac->opad ) )
1854 return( PSA_ERROR_NOT_SUPPORTED );
1855 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001856 return( PSA_ERROR_NOT_SUPPORTED );
1857
Gilles Peskined223b522018-06-11 18:12:58 +02001858 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001859 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001860 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001861 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001862 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001863 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001864 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001865 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001866 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001867 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001868 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001869 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001870 }
Gilles Peskine96889972018-07-12 17:07:03 +02001871 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1872 * but it is permitted. It is common when HMAC is used in HKDF, for
1873 * example. Don't call `memcpy` in the 0-length because `key` could be
1874 * an invalid pointer which would make the behavior undefined. */
1875 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001876 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001877
Gilles Peskined223b522018-06-11 18:12:58 +02001878 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1879 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001880 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001881 ipad[i] ^= 0x36;
1882 memset( ipad + key_length, 0x36, block_size - key_length );
1883
1884 /* Copy the key material from ipad to opad, flipping the requisite bits,
1885 * and filling the rest of opad with the requisite constant. */
1886 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001887 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1888 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001889
Gilles Peskine01126fa2018-07-12 17:04:55 +02001890 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001891 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001892 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001893
Gilles Peskine01126fa2018-07-12 17:04:55 +02001894 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001895
1896cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001897 mbedtls_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001898
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001899 return( status );
1900}
Gilles Peskine248051a2018-06-20 16:09:38 +02001901#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001902
Gilles Peskine89167cb2018-07-08 20:12:23 +02001903static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
1904 psa_key_slot_t key,
1905 psa_algorithm_t alg,
1906 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001907{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001908 psa_status_t status;
1909 key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001910 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001911 psa_key_usage_t usage =
1912 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02001913 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02001914 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001915
Gilles Peskined911eb72018-08-14 15:18:45 +02001916 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001917 if( status != PSA_SUCCESS )
1918 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001919 if( is_sign )
1920 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001921
Gilles Peskine89167cb2018-07-08 20:12:23 +02001922 status = psa_get_key_from_slot( key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001923 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001924 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001925 key_bits = psa_get_key_bits( slot );
1926
Gilles Peskine8c9def32018-02-08 10:02:12 +01001927#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001928 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001929 {
1930 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02001931 mbedtls_cipher_info_from_psa( full_length_alg,
1932 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001933 int ret;
1934 if( cipher_info == NULL )
1935 {
1936 status = PSA_ERROR_NOT_SUPPORTED;
1937 goto exit;
1938 }
1939 operation->mac_size = cipher_info->block_size;
1940 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
1941 status = mbedtls_to_psa_error( ret );
1942 }
1943 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001944#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001945#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001946 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001947 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02001948 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001949 if( hash_alg == 0 )
1950 {
1951 status = PSA_ERROR_NOT_SUPPORTED;
1952 goto exit;
1953 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001954
1955 operation->mac_size = PSA_HASH_SIZE( hash_alg );
1956 /* Sanity check. This shouldn't fail on a valid configuration. */
1957 if( operation->mac_size == 0 ||
1958 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
1959 {
1960 status = PSA_ERROR_NOT_SUPPORTED;
1961 goto exit;
1962 }
1963
Gilles Peskine01126fa2018-07-12 17:04:55 +02001964 if( slot->type != PSA_KEY_TYPE_HMAC )
1965 {
1966 status = PSA_ERROR_INVALID_ARGUMENT;
1967 goto exit;
1968 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001969
Gilles Peskine01126fa2018-07-12 17:04:55 +02001970 status = psa_hmac_setup_internal( &operation->ctx.hmac,
1971 slot->data.raw.data,
1972 slot->data.raw.bytes,
1973 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001974 }
1975 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001976#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001977 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00001978 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02001979 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001980 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001981
Gilles Peskined911eb72018-08-14 15:18:45 +02001982 if( truncated == 0 )
1983 {
1984 /* The "normal" case: untruncated algorithm. Nothing to do. */
1985 }
1986 else if( truncated < 4 )
1987 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02001988 /* A very short MAC is too short for security since it can be
1989 * brute-forced. Ancient protocols with 32-bit MACs do exist,
1990 * so we make this our minimum, even though 32 bits is still
1991 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02001992 status = PSA_ERROR_NOT_SUPPORTED;
1993 }
1994 else if( truncated > operation->mac_size )
1995 {
1996 /* It's impossible to "truncate" to a larger length. */
1997 status = PSA_ERROR_INVALID_ARGUMENT;
1998 }
1999 else
2000 operation->mac_size = truncated;
2001
Gilles Peskinefbfac682018-07-08 20:51:54 +02002002exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002003 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002004 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002005 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002006 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002007 else
2008 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002009 operation->key_set = 1;
2010 }
2011 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002012}
2013
Gilles Peskine89167cb2018-07-08 20:12:23 +02002014psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
2015 psa_key_slot_t key,
2016 psa_algorithm_t alg )
2017{
2018 return( psa_mac_setup( operation, key, alg, 1 ) );
2019}
2020
2021psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
2022 psa_key_slot_t key,
2023 psa_algorithm_t alg )
2024{
2025 return( psa_mac_setup( operation, key, alg, 0 ) );
2026}
2027
Gilles Peskine8c9def32018-02-08 10:02:12 +01002028psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2029 const uint8_t *input,
2030 size_t input_length )
2031{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002032 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002033 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002034 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002035 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002036 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002037 operation->has_input = 1;
2038
Gilles Peskine8c9def32018-02-08 10:02:12 +01002039#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002040 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002041 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002042 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2043 input, input_length );
2044 status = mbedtls_to_psa_error( ret );
2045 }
2046 else
2047#endif /* MBEDTLS_CMAC_C */
2048#if defined(MBEDTLS_MD_C)
2049 if( PSA_ALG_IS_HMAC( operation->alg ) )
2050 {
2051 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2052 input_length );
2053 }
2054 else
2055#endif /* MBEDTLS_MD_C */
2056 {
2057 /* This shouldn't happen if `operation` was initialized by
2058 * a setup function. */
2059 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002060 }
2061
Gilles Peskinefbfac682018-07-08 20:51:54 +02002062cleanup:
2063 if( status != PSA_SUCCESS )
2064 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002065 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002066}
2067
Gilles Peskine01126fa2018-07-12 17:04:55 +02002068#if defined(MBEDTLS_MD_C)
2069static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2070 uint8_t *mac,
2071 size_t mac_size )
2072{
2073 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2074 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2075 size_t hash_size = 0;
2076 size_t block_size = psa_get_hash_block_size( hash_alg );
2077 psa_status_t status;
2078
Gilles Peskine01126fa2018-07-12 17:04:55 +02002079 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2080 if( status != PSA_SUCCESS )
2081 return( status );
2082 /* From here on, tmp needs to be wiped. */
2083
2084 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2085 if( status != PSA_SUCCESS )
2086 goto exit;
2087
2088 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2089 if( status != PSA_SUCCESS )
2090 goto exit;
2091
2092 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2093 if( status != PSA_SUCCESS )
2094 goto exit;
2095
Gilles Peskined911eb72018-08-14 15:18:45 +02002096 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2097 if( status != PSA_SUCCESS )
2098 goto exit;
2099
2100 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002101
2102exit:
2103 mbedtls_zeroize( tmp, hash_size );
2104 return( status );
2105}
2106#endif /* MBEDTLS_MD_C */
2107
mohammad16036df908f2018-04-02 08:34:15 -07002108static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002109 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002110 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002111{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002112 if( ! operation->key_set )
2113 return( PSA_ERROR_BAD_STATE );
2114 if( operation->iv_required && ! operation->iv_set )
2115 return( PSA_ERROR_BAD_STATE );
2116
Gilles Peskine8c9def32018-02-08 10:02:12 +01002117 if( mac_size < operation->mac_size )
2118 return( PSA_ERROR_BUFFER_TOO_SMALL );
2119
Gilles Peskine8c9def32018-02-08 10:02:12 +01002120#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002121 if( operation->alg == PSA_ALG_CMAC )
2122 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002123 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2124 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2125 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002126 memcpy( mac, tmp, operation->mac_size );
Gilles Peskined911eb72018-08-14 15:18:45 +02002127 mbedtls_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002128 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002129 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002130 else
2131#endif /* MBEDTLS_CMAC_C */
2132#if defined(MBEDTLS_MD_C)
2133 if( PSA_ALG_IS_HMAC( operation->alg ) )
2134 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002135 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002136 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002137 }
2138 else
2139#endif /* MBEDTLS_MD_C */
2140 {
2141 /* This shouldn't happen if `operation` was initialized by
2142 * a setup function. */
2143 return( PSA_ERROR_BAD_STATE );
2144 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002145}
2146
Gilles Peskineacd4be32018-07-08 19:56:25 +02002147psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2148 uint8_t *mac,
2149 size_t mac_size,
2150 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002151{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002152 psa_status_t status;
2153
2154 /* Fill the output buffer with something that isn't a valid mac
2155 * (barring an attack on the mac and deliberately-crafted input),
2156 * in case the caller doesn't check the return status properly. */
2157 *mac_length = mac_size;
2158 /* If mac_size is 0 then mac may be NULL and then the
2159 * call to memset would have undefined behavior. */
2160 if( mac_size != 0 )
2161 memset( mac, '!', mac_size );
2162
Gilles Peskine89167cb2018-07-08 20:12:23 +02002163 if( ! operation->is_sign )
2164 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002165 status = PSA_ERROR_BAD_STATE;
2166 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002167 }
mohammad16036df908f2018-04-02 08:34:15 -07002168
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002169 status = psa_mac_finish_internal( operation, mac, mac_size );
2170
2171cleanup:
2172 if( status == PSA_SUCCESS )
2173 {
2174 status = psa_mac_abort( operation );
2175 if( status == PSA_SUCCESS )
2176 *mac_length = operation->mac_size;
2177 else
2178 memset( mac, '!', mac_size );
2179 }
2180 else
2181 psa_mac_abort( operation );
2182 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002183}
2184
Gilles Peskineacd4be32018-07-08 19:56:25 +02002185psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2186 const uint8_t *mac,
2187 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002188{
Gilles Peskine828ed142018-06-18 23:25:51 +02002189 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002190 psa_status_t status;
2191
Gilles Peskine89167cb2018-07-08 20:12:23 +02002192 if( operation->is_sign )
2193 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002194 status = PSA_ERROR_BAD_STATE;
2195 goto cleanup;
2196 }
2197 if( operation->mac_size != mac_length )
2198 {
2199 status = PSA_ERROR_INVALID_SIGNATURE;
2200 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002201 }
mohammad16036df908f2018-04-02 08:34:15 -07002202
2203 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002204 actual_mac, sizeof( actual_mac ) );
2205
2206 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2207 status = PSA_ERROR_INVALID_SIGNATURE;
2208
2209cleanup:
2210 if( status == PSA_SUCCESS )
2211 status = psa_mac_abort( operation );
2212 else
2213 psa_mac_abort( operation );
2214
Gilles Peskine99b7d6b2018-08-21 14:56:19 +02002215 mbedtls_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002216
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002217 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002218}
2219
2220
Gilles Peskine20035e32018-02-03 22:44:14 +01002221
Gilles Peskine20035e32018-02-03 22:44:14 +01002222/****************************************************************/
2223/* Asymmetric cryptography */
2224/****************************************************************/
2225
Gilles Peskine2b450e32018-06-27 15:42:46 +02002226#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002227/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002228 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002229static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2230 size_t hash_length,
2231 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002232{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002233 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002234 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002235 *md_alg = mbedtls_md_get_type( md_info );
2236
2237 /* The Mbed TLS RSA module uses an unsigned int for hash length
2238 * parameters. Validate that it fits so that we don't risk an
2239 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002240#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002241 if( hash_length > UINT_MAX )
2242 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002243#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002244
2245#if defined(MBEDTLS_PKCS1_V15)
2246 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2247 * must be correct. */
2248 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2249 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002250 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002251 if( md_info == NULL )
2252 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002253 if( mbedtls_md_get_size( md_info ) != hash_length )
2254 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002255 }
2256#endif /* MBEDTLS_PKCS1_V15 */
2257
2258#if defined(MBEDTLS_PKCS1_V21)
2259 /* PSS requires a hash internally. */
2260 if( PSA_ALG_IS_RSA_PSS( alg ) )
2261 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002262 if( md_info == NULL )
2263 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002264 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002265#endif /* MBEDTLS_PKCS1_V21 */
2266
Gilles Peskine61b91d42018-06-08 16:09:36 +02002267 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002268}
2269
Gilles Peskine2b450e32018-06-27 15:42:46 +02002270static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2271 psa_algorithm_t alg,
2272 const uint8_t *hash,
2273 size_t hash_length,
2274 uint8_t *signature,
2275 size_t signature_size,
2276 size_t *signature_length )
2277{
2278 psa_status_t status;
2279 int ret;
2280 mbedtls_md_type_t md_alg;
2281
2282 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2283 if( status != PSA_SUCCESS )
2284 return( status );
2285
Gilles Peskine630a18a2018-06-29 17:49:35 +02002286 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002287 return( PSA_ERROR_BUFFER_TOO_SMALL );
2288
2289#if defined(MBEDTLS_PKCS1_V15)
2290 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2291 {
2292 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2293 MBEDTLS_MD_NONE );
2294 ret = mbedtls_rsa_pkcs1_sign( rsa,
2295 mbedtls_ctr_drbg_random,
2296 &global_data.ctr_drbg,
2297 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002298 md_alg,
2299 (unsigned int) hash_length,
2300 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002301 signature );
2302 }
2303 else
2304#endif /* MBEDTLS_PKCS1_V15 */
2305#if defined(MBEDTLS_PKCS1_V21)
2306 if( PSA_ALG_IS_RSA_PSS( alg ) )
2307 {
2308 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2309 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2310 mbedtls_ctr_drbg_random,
2311 &global_data.ctr_drbg,
2312 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002313 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002314 (unsigned int) hash_length,
2315 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002316 signature );
2317 }
2318 else
2319#endif /* MBEDTLS_PKCS1_V21 */
2320 {
2321 return( PSA_ERROR_INVALID_ARGUMENT );
2322 }
2323
2324 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002325 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002326 return( mbedtls_to_psa_error( ret ) );
2327}
2328
2329static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2330 psa_algorithm_t alg,
2331 const uint8_t *hash,
2332 size_t hash_length,
2333 const uint8_t *signature,
2334 size_t signature_length )
2335{
2336 psa_status_t status;
2337 int ret;
2338 mbedtls_md_type_t md_alg;
2339
2340 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2341 if( status != PSA_SUCCESS )
2342 return( status );
2343
Gilles Peskine630a18a2018-06-29 17:49:35 +02002344 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002345 return( PSA_ERROR_BUFFER_TOO_SMALL );
2346
2347#if defined(MBEDTLS_PKCS1_V15)
2348 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2349 {
2350 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2351 MBEDTLS_MD_NONE );
2352 ret = mbedtls_rsa_pkcs1_verify( rsa,
2353 mbedtls_ctr_drbg_random,
2354 &global_data.ctr_drbg,
2355 MBEDTLS_RSA_PUBLIC,
2356 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002357 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002358 hash,
2359 signature );
2360 }
2361 else
2362#endif /* MBEDTLS_PKCS1_V15 */
2363#if defined(MBEDTLS_PKCS1_V21)
2364 if( PSA_ALG_IS_RSA_PSS( alg ) )
2365 {
2366 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2367 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2368 mbedtls_ctr_drbg_random,
2369 &global_data.ctr_drbg,
2370 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002371 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002372 (unsigned int) hash_length,
2373 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002374 signature );
2375 }
2376 else
2377#endif /* MBEDTLS_PKCS1_V21 */
2378 {
2379 return( PSA_ERROR_INVALID_ARGUMENT );
2380 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002381
2382 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2383 * the rest of the signature is invalid". This has little use in
2384 * practice and PSA doesn't report this distinction. */
2385 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2386 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002387 return( mbedtls_to_psa_error( ret ) );
2388}
2389#endif /* MBEDTLS_RSA_C */
2390
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002391#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002392/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2393 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2394 * (even though these functions don't modify it). */
2395static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2396 psa_algorithm_t alg,
2397 const uint8_t *hash,
2398 size_t hash_length,
2399 uint8_t *signature,
2400 size_t signature_size,
2401 size_t *signature_length )
2402{
2403 int ret;
2404 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002405 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002406 mbedtls_mpi_init( &r );
2407 mbedtls_mpi_init( &s );
2408
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002409 if( signature_size < 2 * curve_bytes )
2410 {
2411 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2412 goto cleanup;
2413 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002414
Gilles Peskinea05219c2018-11-16 16:02:56 +01002415#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002416 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2417 {
2418 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2419 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2420 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2421 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2422 hash, hash_length,
2423 md_alg ) );
2424 }
2425 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002426#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002427 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002428 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002429 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2430 hash, hash_length,
2431 mbedtls_ctr_drbg_random,
2432 &global_data.ctr_drbg ) );
2433 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002434
2435 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2436 signature,
2437 curve_bytes ) );
2438 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2439 signature + curve_bytes,
2440 curve_bytes ) );
2441
2442cleanup:
2443 mbedtls_mpi_free( &r );
2444 mbedtls_mpi_free( &s );
2445 if( ret == 0 )
2446 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002447 return( mbedtls_to_psa_error( ret ) );
2448}
2449
2450static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2451 const uint8_t *hash,
2452 size_t hash_length,
2453 const uint8_t *signature,
2454 size_t signature_length )
2455{
2456 int ret;
2457 mbedtls_mpi r, s;
2458 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2459 mbedtls_mpi_init( &r );
2460 mbedtls_mpi_init( &s );
2461
2462 if( signature_length != 2 * curve_bytes )
2463 return( PSA_ERROR_INVALID_SIGNATURE );
2464
2465 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2466 signature,
2467 curve_bytes ) );
2468 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2469 signature + curve_bytes,
2470 curve_bytes ) );
2471
2472 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2473 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002474
2475cleanup:
2476 mbedtls_mpi_free( &r );
2477 mbedtls_mpi_free( &s );
2478 return( mbedtls_to_psa_error( ret ) );
2479}
2480#endif /* MBEDTLS_ECDSA_C */
2481
Gilles Peskine61b91d42018-06-08 16:09:36 +02002482psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
2483 psa_algorithm_t alg,
2484 const uint8_t *hash,
2485 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002486 uint8_t *signature,
2487 size_t signature_size,
2488 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002489{
2490 key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002491 psa_status_t status;
2492
2493 *signature_length = signature_size;
2494
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002495 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_SIGN, alg );
2496 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002497 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002498 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002499 {
2500 status = PSA_ERROR_INVALID_ARGUMENT;
2501 goto exit;
2502 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002503
Gilles Peskine20035e32018-02-03 22:44:14 +01002504#if defined(MBEDTLS_RSA_C)
2505 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2506 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002507 status = psa_rsa_sign( slot->data.rsa,
2508 alg,
2509 hash, hash_length,
2510 signature, signature_size,
2511 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002512 }
2513 else
2514#endif /* defined(MBEDTLS_RSA_C) */
2515#if defined(MBEDTLS_ECP_C)
2516 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2517 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002518#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002519 if(
2520#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2521 PSA_ALG_IS_ECDSA( alg )
2522#else
2523 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2524#endif
2525 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002526 status = psa_ecdsa_sign( slot->data.ecp,
2527 alg,
2528 hash, hash_length,
2529 signature, signature_size,
2530 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002531 else
2532#endif /* defined(MBEDTLS_ECDSA_C) */
2533 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002534 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002535 }
itayzafrir5c753392018-05-08 11:18:38 +03002536 }
2537 else
2538#endif /* defined(MBEDTLS_ECP_C) */
2539 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002540 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002541 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002542
2543exit:
2544 /* Fill the unused part of the output buffer (the whole buffer on error,
2545 * the trailing part on success) with something that isn't a valid mac
2546 * (barring an attack on the mac and deliberately-crafted input),
2547 * in case the caller doesn't check the return status properly. */
2548 if( status == PSA_SUCCESS )
2549 memset( signature + *signature_length, '!',
2550 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002551 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002552 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002553 /* If signature_size is 0 then we have nothing to do. We must not call
2554 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002555 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002556}
2557
2558psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
2559 psa_algorithm_t alg,
2560 const uint8_t *hash,
2561 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002562 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002563 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002564{
2565 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002566 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002567
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002568 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_VERIFY, alg );
2569 if( status != PSA_SUCCESS )
2570 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002571
Gilles Peskine61b91d42018-06-08 16:09:36 +02002572#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002573 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002574 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002575 return( psa_rsa_verify( slot->data.rsa,
2576 alg,
2577 hash, hash_length,
2578 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002579 }
2580 else
2581#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002582#if defined(MBEDTLS_ECP_C)
2583 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2584 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002585#if defined(MBEDTLS_ECDSA_C)
2586 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002587 return( psa_ecdsa_verify( slot->data.ecp,
2588 hash, hash_length,
2589 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002590 else
2591#endif /* defined(MBEDTLS_ECDSA_C) */
2592 {
2593 return( PSA_ERROR_INVALID_ARGUMENT );
2594 }
itayzafrir5c753392018-05-08 11:18:38 +03002595 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002596 else
2597#endif /* defined(MBEDTLS_ECP_C) */
2598 {
2599 return( PSA_ERROR_NOT_SUPPORTED );
2600 }
2601}
2602
Gilles Peskine072ac562018-06-30 00:21:29 +02002603#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2604static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2605 mbedtls_rsa_context *rsa )
2606{
2607 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2608 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2609 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2610 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2611}
2612#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2613
Gilles Peskine61b91d42018-06-08 16:09:36 +02002614psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
2615 psa_algorithm_t alg,
2616 const uint8_t *input,
2617 size_t input_length,
2618 const uint8_t *salt,
2619 size_t salt_length,
2620 uint8_t *output,
2621 size_t output_size,
2622 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002623{
2624 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002625 psa_status_t status;
2626
Darryl Green5cc689a2018-07-24 15:34:10 +01002627 (void) input;
2628 (void) input_length;
2629 (void) salt;
2630 (void) output;
2631 (void) output_size;
2632
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002633 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002634
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002635 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2636 return( PSA_ERROR_INVALID_ARGUMENT );
2637
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002638 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2639 if( status != PSA_SUCCESS )
2640 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002641 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2642 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002643 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002644
2645#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002646 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002647 {
2648 mbedtls_rsa_context *rsa = slot->data.rsa;
2649 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002650 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002651 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002652#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002653 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002654 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002655 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2656 mbedtls_ctr_drbg_random,
2657 &global_data.ctr_drbg,
2658 MBEDTLS_RSA_PUBLIC,
2659 input_length,
2660 input,
2661 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002662 }
2663 else
2664#endif /* MBEDTLS_PKCS1_V15 */
2665#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002666 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002667 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002668 psa_rsa_oaep_set_padding_mode( alg, rsa );
2669 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2670 mbedtls_ctr_drbg_random,
2671 &global_data.ctr_drbg,
2672 MBEDTLS_RSA_PUBLIC,
2673 salt, salt_length,
2674 input_length,
2675 input,
2676 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002677 }
2678 else
2679#endif /* MBEDTLS_PKCS1_V21 */
2680 {
2681 return( PSA_ERROR_INVALID_ARGUMENT );
2682 }
2683 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002684 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002685 return( mbedtls_to_psa_error( ret ) );
2686 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002687 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002688#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002689 {
2690 return( PSA_ERROR_NOT_SUPPORTED );
2691 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002692}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002693
Gilles Peskine61b91d42018-06-08 16:09:36 +02002694psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
2695 psa_algorithm_t alg,
2696 const uint8_t *input,
2697 size_t input_length,
2698 const uint8_t *salt,
2699 size_t salt_length,
2700 uint8_t *output,
2701 size_t output_size,
2702 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002703{
2704 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002705 psa_status_t status;
2706
Darryl Green5cc689a2018-07-24 15:34:10 +01002707 (void) input;
2708 (void) input_length;
2709 (void) salt;
2710 (void) output;
2711 (void) output_size;
2712
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002713 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002714
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002715 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2716 return( PSA_ERROR_INVALID_ARGUMENT );
2717
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002718 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2719 if( status != PSA_SUCCESS )
2720 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002721 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2722 return( PSA_ERROR_INVALID_ARGUMENT );
2723
2724#if defined(MBEDTLS_RSA_C)
2725 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2726 {
2727 mbedtls_rsa_context *rsa = slot->data.rsa;
2728 int ret;
2729
Gilles Peskine630a18a2018-06-29 17:49:35 +02002730 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002731 return( PSA_ERROR_INVALID_ARGUMENT );
2732
2733#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002734 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002735 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002736 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2737 mbedtls_ctr_drbg_random,
2738 &global_data.ctr_drbg,
2739 MBEDTLS_RSA_PRIVATE,
2740 output_length,
2741 input,
2742 output,
2743 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002744 }
2745 else
2746#endif /* MBEDTLS_PKCS1_V15 */
2747#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002748 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002749 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002750 psa_rsa_oaep_set_padding_mode( alg, rsa );
2751 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2752 mbedtls_ctr_drbg_random,
2753 &global_data.ctr_drbg,
2754 MBEDTLS_RSA_PRIVATE,
2755 salt, salt_length,
2756 output_length,
2757 input,
2758 output,
2759 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002760 }
2761 else
2762#endif /* MBEDTLS_PKCS1_V21 */
2763 {
2764 return( PSA_ERROR_INVALID_ARGUMENT );
2765 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002766
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002767 return( mbedtls_to_psa_error( ret ) );
2768 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002769 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002770#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002771 {
2772 return( PSA_ERROR_NOT_SUPPORTED );
2773 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002774}
Gilles Peskine20035e32018-02-03 22:44:14 +01002775
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002776
2777
mohammad1603503973b2018-03-12 15:59:30 +02002778/****************************************************************/
2779/* Symmetric cryptography */
2780/****************************************************************/
2781
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002782/* Initialize the cipher operation structure. Once this function has been
2783 * called, psa_cipher_abort can run and will do the right thing. */
2784static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2785 psa_algorithm_t alg )
2786{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002787 if( ! PSA_ALG_IS_CIPHER( alg ) )
2788 {
2789 memset( operation, 0, sizeof( *operation ) );
2790 return( PSA_ERROR_INVALID_ARGUMENT );
2791 }
2792
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002793 operation->alg = alg;
2794 operation->key_set = 0;
2795 operation->iv_set = 0;
2796 operation->iv_required = 1;
2797 operation->iv_size = 0;
2798 operation->block_size = 0;
2799 mbedtls_cipher_init( &operation->ctx.cipher );
2800 return( PSA_SUCCESS );
2801}
2802
Gilles Peskinee553c652018-06-04 16:22:46 +02002803static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
2804 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02002805 psa_algorithm_t alg,
2806 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002807{
2808 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2809 psa_status_t status;
2810 key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002811 size_t key_bits;
2812 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002813 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2814 PSA_KEY_USAGE_ENCRYPT :
2815 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002816
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002817 status = psa_cipher_init( operation, alg );
2818 if( status != PSA_SUCCESS )
2819 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002820
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002821 status = psa_get_key_from_slot( key, &slot, usage, alg);
2822 if( status != PSA_SUCCESS )
2823 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002824 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002825
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002826 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002827 if( cipher_info == NULL )
2828 return( PSA_ERROR_NOT_SUPPORTED );
2829
mohammad1603503973b2018-03-12 15:59:30 +02002830 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002831 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002832 {
2833 psa_cipher_abort( operation );
2834 return( mbedtls_to_psa_error( ret ) );
2835 }
2836
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002837#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002838 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002839 {
2840 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2841 unsigned char keys[24];
2842 memcpy( keys, slot->data.raw.data, 16 );
2843 memcpy( keys + 16, slot->data.raw.data, 8 );
2844 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2845 keys,
2846 192, cipher_operation );
2847 }
2848 else
2849#endif
2850 {
2851 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2852 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002853 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002854 }
Moran Peker41deec42018-04-04 15:43:05 +03002855 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002856 {
2857 psa_cipher_abort( operation );
2858 return( mbedtls_to_psa_error( ret ) );
2859 }
2860
mohammad16038481e742018-03-18 13:57:31 +02002861#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002862 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002863 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002864 case PSA_ALG_CBC_NO_PADDING:
2865 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2866 MBEDTLS_PADDING_NONE );
2867 break;
2868 case PSA_ALG_CBC_PKCS7:
2869 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2870 MBEDTLS_PADDING_PKCS7 );
2871 break;
2872 default:
2873 /* The algorithm doesn't involve padding. */
2874 ret = 0;
2875 break;
2876 }
2877 if( ret != 0 )
2878 {
2879 psa_cipher_abort( operation );
2880 return( mbedtls_to_psa_error( ret ) );
mohammad16038481e742018-03-18 13:57:31 +02002881 }
2882#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2883
mohammad1603503973b2018-03-12 15:59:30 +02002884 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002885 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2886 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
2887 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02002888 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002889 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002890 }
mohammad1603503973b2018-03-12 15:59:30 +02002891
Moran Peker395db872018-05-31 14:07:14 +03002892 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002893}
2894
Gilles Peskinefe119512018-07-08 21:39:34 +02002895psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
2896 psa_key_slot_t key,
2897 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002898{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002899 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002900}
2901
Gilles Peskinefe119512018-07-08 21:39:34 +02002902psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
2903 psa_key_slot_t key,
2904 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002905{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002906 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002907}
2908
Gilles Peskinefe119512018-07-08 21:39:34 +02002909psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2910 unsigned char *iv,
2911 size_t iv_size,
2912 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002913{
itayzafrir534bd7c2018-08-02 13:56:32 +03002914 psa_status_t status;
2915 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002916 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002917 {
2918 status = PSA_ERROR_BAD_STATE;
2919 goto exit;
2920 }
Moran Peker41deec42018-04-04 15:43:05 +03002921 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002922 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002923 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03002924 goto exit;
2925 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002926 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2927 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002928 if( ret != 0 )
2929 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002930 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002931 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002932 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002933
mohammad16038481e742018-03-18 13:57:31 +02002934 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03002935 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002936
Moran Peker395db872018-05-31 14:07:14 +03002937exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03002938 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03002939 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002940 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002941}
2942
Gilles Peskinefe119512018-07-08 21:39:34 +02002943psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2944 const unsigned char *iv,
2945 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002946{
itayzafrir534bd7c2018-08-02 13:56:32 +03002947 psa_status_t status;
2948 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002949 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002950 {
2951 status = PSA_ERROR_BAD_STATE;
2952 goto exit;
2953 }
Moran Pekera28258c2018-05-29 16:25:04 +03002954 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002955 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002956 status = PSA_ERROR_INVALID_ARGUMENT;
2957 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03002958 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002959 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
2960 status = mbedtls_to_psa_error( ret );
2961exit:
2962 if( status == PSA_SUCCESS )
2963 operation->iv_set = 1;
2964 else
mohammad1603503973b2018-03-12 15:59:30 +02002965 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002966 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002967}
2968
Gilles Peskinee553c652018-06-04 16:22:46 +02002969psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2970 const uint8_t *input,
2971 size_t input_length,
2972 unsigned char *output,
2973 size_t output_size,
2974 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002975{
itayzafrir534bd7c2018-08-02 13:56:32 +03002976 psa_status_t status;
2977 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002978 size_t expected_output_size;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002979 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002980 {
2981 /* Take the unprocessed partial block left over from previous
2982 * update calls, if any, plus the input to this call. Remove
2983 * the last partial block, if any. You get the data that will be
2984 * output in this call. */
2985 expected_output_size =
2986 ( operation->ctx.cipher.unprocessed_len + input_length )
2987 / operation->block_size * operation->block_size;
2988 }
2989 else
2990 {
2991 expected_output_size = input_length;
2992 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002993
Gilles Peskine89d789c2018-06-04 17:17:16 +02002994 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03002995 {
2996 status = PSA_ERROR_BUFFER_TOO_SMALL;
2997 goto exit;
2998 }
mohammad160382759612018-03-12 18:16:40 +02002999
mohammad1603503973b2018-03-12 15:59:30 +02003000 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003001 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003002 status = mbedtls_to_psa_error( ret );
3003exit:
3004 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003005 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003006 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003007}
3008
Gilles Peskinee553c652018-06-04 16:22:46 +02003009psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3010 uint8_t *output,
3011 size_t output_size,
3012 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003013{
Janos Follath315b51c2018-07-09 16:04:51 +01003014 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
3015 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003016 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003017
mohammad1603503973b2018-03-12 15:59:30 +02003018 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003019 {
Janos Follath315b51c2018-07-09 16:04:51 +01003020 status = PSA_ERROR_BAD_STATE;
3021 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03003022 }
3023 if( operation->iv_required && ! operation->iv_set )
3024 {
Janos Follath315b51c2018-07-09 16:04:51 +01003025 status = PSA_ERROR_BAD_STATE;
3026 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03003027 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003028
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003029 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003030 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3031 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003032 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003033 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003034 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003035 }
3036
Janos Follath315b51c2018-07-09 16:04:51 +01003037 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3038 temp_output_buffer,
3039 output_length );
3040 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003041 {
Janos Follath315b51c2018-07-09 16:04:51 +01003042 status = mbedtls_to_psa_error( cipher_ret );
3043 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003044 }
Janos Follath315b51c2018-07-09 16:04:51 +01003045
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003046 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003047 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003048 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003049 memcpy( output, temp_output_buffer, *output_length );
3050 else
3051 {
Janos Follath315b51c2018-07-09 16:04:51 +01003052 status = PSA_ERROR_BUFFER_TOO_SMALL;
3053 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003054 }
mohammad1603503973b2018-03-12 15:59:30 +02003055
Janos Follath279ab8e2018-07-09 16:13:21 +01003056 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003057 status = psa_cipher_abort( operation );
3058
3059 return( status );
3060
3061error:
3062
3063 *output_length = 0;
3064
Janos Follath279ab8e2018-07-09 16:13:21 +01003065 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003066 (void) psa_cipher_abort( operation );
3067
3068 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003069}
3070
Gilles Peskinee553c652018-06-04 16:22:46 +02003071psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3072{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003073 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003074 {
3075 /* The object has (apparently) been initialized but it is not
3076 * in use. It's ok to call abort on such an object, and there's
3077 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003078 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003079 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003080
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003081 /* Sanity check (shouldn't happen: operation->alg should
3082 * always have been initialized to a valid value). */
3083 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3084 return( PSA_ERROR_BAD_STATE );
3085
mohammad1603503973b2018-03-12 15:59:30 +02003086 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003087
Moran Peker41deec42018-04-04 15:43:05 +03003088 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003089 operation->key_set = 0;
3090 operation->iv_set = 0;
3091 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003092 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003093 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003094
Moran Peker395db872018-05-31 14:07:14 +03003095 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003096}
3097
Gilles Peskinea0655c32018-04-30 17:06:50 +02003098
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003099
mohammad16038cc1cee2018-03-28 01:21:33 +03003100/****************************************************************/
3101/* Key Policy */
3102/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003103
mohammad160327010052018-07-03 13:16:15 +03003104#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02003105void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003106{
Gilles Peskine803ce742018-06-18 16:07:14 +02003107 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03003108}
3109
Gilles Peskine2d277862018-06-18 15:41:12 +02003110void psa_key_policy_set_usage( psa_key_policy_t *policy,
3111 psa_key_usage_t usage,
3112 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03003113{
mohammad16034eed7572018-03-28 05:14:59 -07003114 policy->usage = usage;
3115 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03003116}
3117
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003118psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003119{
mohammad16036df908f2018-04-02 08:34:15 -07003120 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03003121}
3122
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003123psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003124{
mohammad16036df908f2018-04-02 08:34:15 -07003125 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03003126}
mohammad160327010052018-07-03 13:16:15 +03003127#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003128
Gilles Peskine2d277862018-06-18 15:41:12 +02003129psa_status_t psa_set_key_policy( psa_key_slot_t key,
3130 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003131{
3132 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003133 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003134
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003135 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003136 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003137
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003138 status = psa_get_empty_key_slot( key, &slot );
3139 if( status != PSA_SUCCESS )
3140 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03003141
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003142 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
3143 PSA_KEY_USAGE_ENCRYPT |
3144 PSA_KEY_USAGE_DECRYPT |
3145 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02003146 PSA_KEY_USAGE_VERIFY |
3147 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07003148 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03003149
mohammad16036df908f2018-04-02 08:34:15 -07003150 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003151
3152 return( PSA_SUCCESS );
3153}
3154
Gilles Peskine2d277862018-06-18 15:41:12 +02003155psa_status_t psa_get_key_policy( psa_key_slot_t key,
3156 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003157{
3158 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003159 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003160
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003161 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003162 return( PSA_ERROR_INVALID_ARGUMENT );
3163
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003164 status = psa_get_key_slot( key, &slot );
3165 if( status != PSA_SUCCESS )
3166 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003167
mohammad16036df908f2018-04-02 08:34:15 -07003168 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003169
3170 return( PSA_SUCCESS );
3171}
Gilles Peskine20035e32018-02-03 22:44:14 +01003172
Gilles Peskinea0655c32018-04-30 17:06:50 +02003173
3174
mohammad1603804cd712018-03-20 22:44:08 +02003175/****************************************************************/
3176/* Key Lifetime */
3177/****************************************************************/
3178
Gilles Peskine2d277862018-06-18 15:41:12 +02003179psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
3180 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003181{
3182 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003183 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003184
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003185 status = psa_get_key_slot( key, &slot );
3186 if( status != PSA_SUCCESS )
3187 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003188
mohammad1603804cd712018-03-20 22:44:08 +02003189 *lifetime = slot->lifetime;
3190
3191 return( PSA_SUCCESS );
3192}
3193
Gilles Peskine2d277862018-06-18 15:41:12 +02003194psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
Jaeden Amero65fb2362018-06-26 13:55:30 +01003195 psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003196{
3197 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003198 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003199
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003200 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
3201 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
Darryl Greend49a4992018-06-18 17:27:26 +01003202 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE )
mohammad1603ba178512018-03-21 04:35:20 -07003203 return( PSA_ERROR_INVALID_ARGUMENT );
3204
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003205 status = psa_get_empty_key_slot( key, &slot );
3206 if( status != PSA_SUCCESS )
3207 return( status );
mohammad1603804cd712018-03-20 22:44:08 +02003208
Darryl Greend49a4992018-06-18 17:27:26 +01003209 if( lifetime == PSA_KEY_LIFETIME_WRITE_ONCE )
mohammad1603ba178512018-03-21 04:35:20 -07003210 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003211
Darryl Greend49a4992018-06-18 17:27:26 +01003212#if !defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
3213 if( lifetime == PSA_KEY_LIFETIME_PERSISTENT )
3214 return( PSA_ERROR_NOT_SUPPORTED );
3215#endif
3216
mohammad1603060ad8a2018-03-20 14:28:38 -07003217 slot->lifetime = lifetime;
Gilles Peskine69f976b2018-11-30 18:46:56 +01003218 slot->persistent_storage_id = key;
mohammad1603804cd712018-03-20 22:44:08 +02003219
3220 return( PSA_SUCCESS );
3221}
3222
Gilles Peskine20035e32018-02-03 22:44:14 +01003223
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003224
mohammad16035955c982018-04-26 00:53:03 +03003225/****************************************************************/
3226/* AEAD */
3227/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003228
Gilles Peskineedf9a652018-08-17 18:11:56 +02003229typedef struct
3230{
3231 key_slot_t *slot;
3232 const mbedtls_cipher_info_t *cipher_info;
3233 union
3234 {
3235#if defined(MBEDTLS_CCM_C)
3236 mbedtls_ccm_context ccm;
3237#endif /* MBEDTLS_CCM_C */
3238#if defined(MBEDTLS_GCM_C)
3239 mbedtls_gcm_context gcm;
3240#endif /* MBEDTLS_GCM_C */
3241 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003242 psa_algorithm_t core_alg;
3243 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003244 uint8_t tag_length;
3245} aead_operation_t;
3246
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003247static void psa_aead_abort( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003248{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003249 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003250 {
3251#if defined(MBEDTLS_CCM_C)
3252 case PSA_ALG_CCM:
3253 mbedtls_ccm_free( &operation->ctx.ccm );
3254 break;
3255#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003256#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003257 case PSA_ALG_GCM:
3258 mbedtls_gcm_free( &operation->ctx.gcm );
3259 break;
3260#endif /* MBEDTLS_GCM_C */
3261 }
3262}
3263
3264static psa_status_t psa_aead_setup( aead_operation_t *operation,
3265 psa_key_slot_t key,
3266 psa_key_usage_t usage,
3267 psa_algorithm_t alg )
3268{
3269 psa_status_t status;
3270 size_t key_bits;
3271 mbedtls_cipher_id_t cipher_id;
3272
3273 status = psa_get_key_from_slot( key, &operation->slot, usage, alg );
3274 if( status != PSA_SUCCESS )
3275 return( status );
3276
3277 key_bits = psa_get_key_bits( operation->slot );
3278
3279 operation->cipher_info =
3280 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3281 &cipher_id );
3282 if( operation->cipher_info == NULL )
3283 return( PSA_ERROR_NOT_SUPPORTED );
3284
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003285 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003286 {
3287#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003288 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3289 operation->core_alg = PSA_ALG_CCM;
3290 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003291 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3292 return( PSA_ERROR_INVALID_ARGUMENT );
3293 mbedtls_ccm_init( &operation->ctx.ccm );
3294 status = mbedtls_to_psa_error(
3295 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3296 operation->slot->data.raw.data,
3297 (unsigned int) key_bits ) );
3298 if( status != 0 )
3299 goto cleanup;
3300 break;
3301#endif /* MBEDTLS_CCM_C */
3302
3303#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003304 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3305 operation->core_alg = PSA_ALG_GCM;
3306 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003307 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3308 return( PSA_ERROR_INVALID_ARGUMENT );
3309 mbedtls_gcm_init( &operation->ctx.gcm );
3310 status = mbedtls_to_psa_error(
3311 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3312 operation->slot->data.raw.data,
3313 (unsigned int) key_bits ) );
3314 break;
3315#endif /* MBEDTLS_GCM_C */
3316
3317 default:
3318 return( PSA_ERROR_NOT_SUPPORTED );
3319 }
3320
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003321 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3322 {
3323 status = PSA_ERROR_INVALID_ARGUMENT;
3324 goto cleanup;
3325 }
3326 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3327 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3328 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3329 * In both cases, mbedtls_xxx will validate the tag length below. */
3330
Gilles Peskineedf9a652018-08-17 18:11:56 +02003331 return( PSA_SUCCESS );
3332
3333cleanup:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003334 psa_aead_abort( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003335 return( status );
3336}
3337
mohammad16035955c982018-04-26 00:53:03 +03003338psa_status_t psa_aead_encrypt( psa_key_slot_t key,
3339 psa_algorithm_t alg,
3340 const uint8_t *nonce,
3341 size_t nonce_length,
3342 const uint8_t *additional_data,
3343 size_t additional_data_length,
3344 const uint8_t *plaintext,
3345 size_t plaintext_length,
3346 uint8_t *ciphertext,
3347 size_t ciphertext_size,
3348 size_t *ciphertext_length )
3349{
mohammad16035955c982018-04-26 00:53:03 +03003350 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003351 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003352 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003353
mohammad1603f08a5502018-06-03 15:05:47 +03003354 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003355
Gilles Peskineedf9a652018-08-17 18:11:56 +02003356 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003357 if( status != PSA_SUCCESS )
3358 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003359
Gilles Peskineedf9a652018-08-17 18:11:56 +02003360 /* For all currently supported modes, the tag is at the end of the
3361 * ciphertext. */
3362 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3363 {
3364 status = PSA_ERROR_BUFFER_TOO_SMALL;
3365 goto exit;
3366 }
3367 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003368
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003369#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003370 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003371 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003372 status = mbedtls_to_psa_error(
3373 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3374 MBEDTLS_GCM_ENCRYPT,
3375 plaintext_length,
3376 nonce, nonce_length,
3377 additional_data, additional_data_length,
3378 plaintext, ciphertext,
3379 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003380 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003381 else
3382#endif /* MBEDTLS_GCM_C */
3383#if defined(MBEDTLS_CCM_C)
3384 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003385 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003386 status = mbedtls_to_psa_error(
3387 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3388 plaintext_length,
3389 nonce, nonce_length,
3390 additional_data,
3391 additional_data_length,
3392 plaintext, ciphertext,
3393 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003394 }
mohammad16035c8845f2018-05-09 05:40:09 -07003395 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003396#endif /* MBEDTLS_CCM_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003397 {
mohammad1603554faad2018-06-03 15:07:38 +03003398 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003399 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003400
Gilles Peskineedf9a652018-08-17 18:11:56 +02003401 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3402 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003403
Gilles Peskineedf9a652018-08-17 18:11:56 +02003404exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003405 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003406 if( status == PSA_SUCCESS )
3407 *ciphertext_length = plaintext_length + operation.tag_length;
3408 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003409}
3410
Gilles Peskineee652a32018-06-01 19:23:52 +02003411/* Locate the tag in a ciphertext buffer containing the encrypted data
3412 * followed by the tag. Return the length of the part preceding the tag in
3413 * *plaintext_length. This is the size of the plaintext in modes where
3414 * the encrypted data has the same size as the plaintext, such as
3415 * CCM and GCM. */
3416static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3417 const uint8_t *ciphertext,
3418 size_t ciphertext_length,
3419 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003420 const uint8_t **p_tag )
3421{
3422 size_t payload_length;
3423 if( tag_length > ciphertext_length )
3424 return( PSA_ERROR_INVALID_ARGUMENT );
3425 payload_length = ciphertext_length - tag_length;
3426 if( payload_length > plaintext_size )
3427 return( PSA_ERROR_BUFFER_TOO_SMALL );
3428 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003429 return( PSA_SUCCESS );
3430}
3431
mohammad16035955c982018-04-26 00:53:03 +03003432psa_status_t psa_aead_decrypt( psa_key_slot_t key,
3433 psa_algorithm_t alg,
3434 const uint8_t *nonce,
3435 size_t nonce_length,
3436 const uint8_t *additional_data,
3437 size_t additional_data_length,
3438 const uint8_t *ciphertext,
3439 size_t ciphertext_length,
3440 uint8_t *plaintext,
3441 size_t plaintext_size,
3442 size_t *plaintext_length )
3443{
mohammad16035955c982018-04-26 00:53:03 +03003444 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003445 aead_operation_t operation;
3446 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003447
Gilles Peskineee652a32018-06-01 19:23:52 +02003448 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003449
Gilles Peskineedf9a652018-08-17 18:11:56 +02003450 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003451 if( status != PSA_SUCCESS )
3452 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003453
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003454#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003455 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003456 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003457 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003458 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003459 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003460 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003461 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003462
Gilles Peskineedf9a652018-08-17 18:11:56 +02003463 status = mbedtls_to_psa_error(
3464 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3465 ciphertext_length - operation.tag_length,
3466 nonce, nonce_length,
3467 additional_data,
3468 additional_data_length,
3469 tag, operation.tag_length,
3470 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003471 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003472 else
3473#endif /* MBEDTLS_GCM_C */
3474#if defined(MBEDTLS_CCM_C)
3475 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003476 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003477 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003478 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003479 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003480 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003481 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003482
Gilles Peskineedf9a652018-08-17 18:11:56 +02003483 status = mbedtls_to_psa_error(
3484 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3485 ciphertext_length - operation.tag_length,
3486 nonce, nonce_length,
3487 additional_data,
3488 additional_data_length,
3489 ciphertext, plaintext,
3490 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003491 }
mohammad160339574652018-06-01 04:39:53 -07003492 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003493#endif /* MBEDTLS_CCM_C */
mohammad160339574652018-06-01 04:39:53 -07003494 {
mohammad1603554faad2018-06-03 15:07:38 +03003495 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003496 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003497
Gilles Peskineedf9a652018-08-17 18:11:56 +02003498 if( status != PSA_SUCCESS && plaintext_size != 0 )
3499 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003500
Gilles Peskineedf9a652018-08-17 18:11:56 +02003501exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003502 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003503 if( status == PSA_SUCCESS )
3504 *plaintext_length = ciphertext_length - operation.tag_length;
3505 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003506}
3507
Gilles Peskinea0655c32018-04-30 17:06:50 +02003508
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003509
Gilles Peskine20035e32018-02-03 22:44:14 +01003510/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003511/* Generators */
3512/****************************************************************/
3513
3514psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3515{
3516 psa_status_t status = PSA_SUCCESS;
3517 if( generator->alg == 0 )
3518 {
3519 /* The object has (apparently) been initialized but it is not
3520 * in use. It's ok to call abort on such an object, and there's
3521 * nothing to do. */
3522 }
3523 else
Gilles Peskine751d9652018-09-18 12:05:44 +02003524 if( generator->alg == PSA_ALG_SELECT_RAW )
3525 {
3526 if( generator->ctx.buffer.data != NULL )
3527 {
3528 mbedtls_zeroize( generator->ctx.buffer.data,
3529 generator->ctx.buffer.size );
3530 mbedtls_free( generator->ctx.buffer.data );
3531 }
3532 }
3533 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003534#if defined(MBEDTLS_MD_C)
3535 if( PSA_ALG_IS_HKDF( generator->alg ) )
3536 {
3537 mbedtls_free( generator->ctx.hkdf.info );
3538 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3539 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003540 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3541 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
3542 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003543 {
3544 if( generator->ctx.tls12_prf.key != NULL )
3545 {
3546 mbedtls_zeroize( generator->ctx.tls12_prf.key,
3547 generator->ctx.tls12_prf.key_len );
3548 mbedtls_free( generator->ctx.tls12_prf.key );
3549 }
Hanno Becker580fba12018-11-13 20:50:45 +00003550
3551 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
3552 {
3553 mbedtls_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
3554 generator->ctx.tls12_prf.Ai_with_seed_len );
3555 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
3556 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003557 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003558 else
3559#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003560 {
3561 status = PSA_ERROR_BAD_STATE;
3562 }
3563 memset( generator, 0, sizeof( *generator ) );
3564 return( status );
3565}
3566
3567
3568psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3569 size_t *capacity)
3570{
3571 *capacity = generator->capacity;
3572 return( PSA_SUCCESS );
3573}
3574
Gilles Peskinebef7f142018-07-12 17:22:21 +02003575#if defined(MBEDTLS_MD_C)
3576/* Read some bytes from an HKDF-based generator. This performs a chunk
3577 * of the expand phase of the HKDF algorithm. */
3578static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3579 psa_algorithm_t hash_alg,
3580 uint8_t *output,
3581 size_t output_length )
3582{
3583 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3584 psa_status_t status;
3585
3586 while( output_length != 0 )
3587 {
3588 /* Copy what remains of the current block */
3589 uint8_t n = hash_length - hkdf->offset_in_block;
3590 if( n > output_length )
3591 n = (uint8_t) output_length;
3592 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3593 output += n;
3594 output_length -= n;
3595 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003596 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003597 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003598 /* We can't be wanting more output after block 0xff, otherwise
3599 * the capacity check in psa_generator_read() would have
3600 * prevented this call. It could happen only if the generator
3601 * object was corrupted or if this function is called directly
3602 * inside the library. */
3603 if( hkdf->block_number == 0xff )
3604 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003605
3606 /* We need a new block */
3607 ++hkdf->block_number;
3608 hkdf->offset_in_block = 0;
3609 status = psa_hmac_setup_internal( &hkdf->hmac,
3610 hkdf->prk, hash_length,
3611 hash_alg );
3612 if( status != PSA_SUCCESS )
3613 return( status );
3614 if( hkdf->block_number != 1 )
3615 {
3616 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3617 hkdf->output_block,
3618 hash_length );
3619 if( status != PSA_SUCCESS )
3620 return( status );
3621 }
3622 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3623 hkdf->info,
3624 hkdf->info_length );
3625 if( status != PSA_SUCCESS )
3626 return( status );
3627 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3628 &hkdf->block_number, 1 );
3629 if( status != PSA_SUCCESS )
3630 return( status );
3631 status = psa_hmac_finish_internal( &hkdf->hmac,
3632 hkdf->output_block,
3633 sizeof( hkdf->output_block ) );
3634 if( status != PSA_SUCCESS )
3635 return( status );
3636 }
3637
3638 return( PSA_SUCCESS );
3639}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003640
3641static psa_status_t psa_generator_tls12_prf_generate_next_block(
3642 psa_tls12_prf_generator_t *tls12_prf,
3643 psa_algorithm_t alg )
3644{
3645 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3646 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3647 psa_hmac_internal_data hmac;
3648 psa_status_t status, cleanup_status;
3649
Hanno Becker3b339e22018-11-13 20:56:14 +00003650 unsigned char *Ai;
3651 size_t Ai_len;
3652
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003653 /* We can't be wanting more output after block 0xff, otherwise
3654 * the capacity check in psa_generator_read() would have
3655 * prevented this call. It could happen only if the generator
3656 * object was corrupted or if this function is called directly
3657 * inside the library. */
3658 if( tls12_prf->block_number == 0xff )
3659 return( PSA_ERROR_BAD_STATE );
3660
3661 /* We need a new block */
3662 ++tls12_prf->block_number;
3663 tls12_prf->offset_in_block = 0;
3664
3665 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
3666 *
3667 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
3668 *
3669 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
3670 * HMAC_hash(secret, A(2) + seed) +
3671 * HMAC_hash(secret, A(3) + seed) + ...
3672 *
3673 * A(0) = seed
3674 * A(i) = HMAC_hash( secret, A(i-1) )
3675 *
3676 * The `psa_tls12_prf_generator` structures saves the block
3677 * `HMAC_hash(secret, A(i) + seed)` from which the output
3678 * is currently extracted as `output_block`, while
3679 * `A(i) + seed` is stored in `Ai_with_seed`.
3680 *
3681 * Generating a new block means recalculating `Ai_with_seed`
3682 * from the A(i)-part of it, and afterwards recalculating
3683 * `output_block`.
3684 *
3685 * A(0) is computed at setup time.
3686 *
3687 */
3688
3689 psa_hmac_init_internal( &hmac );
3690
3691 /* We must distinguish the calculation of A(1) from those
3692 * of A(2) and higher, because A(0)=seed has a different
3693 * length than the other A(i). */
3694 if( tls12_prf->block_number == 1 )
3695 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003696 Ai = tls12_prf->Ai_with_seed + hash_length;
3697 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003698 }
3699 else
3700 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003701 Ai = tls12_prf->Ai_with_seed;
3702 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003703 }
3704
Hanno Becker3b339e22018-11-13 20:56:14 +00003705 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
3706 status = psa_hmac_setup_internal( &hmac,
3707 tls12_prf->key,
3708 tls12_prf->key_len,
3709 hash_alg );
3710 if( status != PSA_SUCCESS )
3711 goto cleanup;
3712
3713 status = psa_hash_update( &hmac.hash_ctx,
3714 Ai, Ai_len );
3715 if( status != PSA_SUCCESS )
3716 goto cleanup;
3717
3718 status = psa_hmac_finish_internal( &hmac,
3719 tls12_prf->Ai_with_seed,
3720 hash_length );
3721 if( status != PSA_SUCCESS )
3722 goto cleanup;
3723
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003724 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
3725 status = psa_hmac_setup_internal( &hmac,
3726 tls12_prf->key,
3727 tls12_prf->key_len,
3728 hash_alg );
3729 if( status != PSA_SUCCESS )
3730 goto cleanup;
3731
3732 status = psa_hash_update( &hmac.hash_ctx,
3733 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003734 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003735 if( status != PSA_SUCCESS )
3736 goto cleanup;
3737
3738 status = psa_hmac_finish_internal( &hmac,
3739 tls12_prf->output_block,
3740 hash_length );
3741 if( status != PSA_SUCCESS )
3742 goto cleanup;
3743
3744cleanup:
3745
3746 cleanup_status = psa_hmac_abort_internal( &hmac );
3747 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
3748 status = cleanup_status;
3749
3750 return( status );
3751}
3752
3753/* Read some bytes from an TLS-1.2-PRF-based generator.
3754 * See Section 5 of RFC 5246. */
3755static psa_status_t psa_generator_tls12_prf_read(
3756 psa_tls12_prf_generator_t *tls12_prf,
3757 psa_algorithm_t alg,
3758 uint8_t *output,
3759 size_t output_length )
3760{
3761 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3762 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3763 psa_status_t status;
3764
3765 while( output_length != 0 )
3766 {
3767 /* Copy what remains of the current block */
3768 uint8_t n = hash_length - tls12_prf->offset_in_block;
3769
3770 /* Check if we have fully processed the current block. */
3771 if( n == 0 )
3772 {
3773 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
3774 alg );
3775 if( status != PSA_SUCCESS )
3776 return( status );
3777
3778 continue;
3779 }
3780
3781 if( n > output_length )
3782 n = (uint8_t) output_length;
3783 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
3784 n );
3785 output += n;
3786 output_length -= n;
3787 tls12_prf->offset_in_block += n;
3788 }
3789
3790 return( PSA_SUCCESS );
3791}
Gilles Peskinebef7f142018-07-12 17:22:21 +02003792#endif /* MBEDTLS_MD_C */
3793
Gilles Peskineeab56e42018-07-12 17:12:33 +02003794psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3795 uint8_t *output,
3796 size_t output_length )
3797{
3798 psa_status_t status;
3799
3800 if( output_length > generator->capacity )
3801 {
3802 generator->capacity = 0;
3803 /* Go through the error path to wipe all confidential data now
3804 * that the generator object is useless. */
3805 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3806 goto exit;
3807 }
3808 if( output_length == 0 &&
3809 generator->capacity == 0 && generator->alg == 0 )
3810 {
3811 /* Edge case: this is a blank or finished generator, and 0
3812 * bytes were requested. The right error in this case could
3813 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3814 * INSUFFICIENT_CAPACITY, which is right for a finished
3815 * generator, for consistency with the case when
3816 * output_length > 0. */
3817 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3818 }
3819 generator->capacity -= output_length;
3820
Gilles Peskine751d9652018-09-18 12:05:44 +02003821 if( generator->alg == PSA_ALG_SELECT_RAW )
3822 {
Gilles Peskine211a4362018-10-25 22:22:31 +02003823 /* Initially, the capacity of a selection generator is always
3824 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
3825 * abbreviated in this comment as `size`. When the remaining
3826 * capacity is `c`, the next bytes to serve start `c` bytes
3827 * from the end of the buffer, i.e. `size - c` from the
3828 * beginning of the buffer. Since `generator->capacity` was just
3829 * decremented above, we need to serve the bytes from
3830 * `size - generator->capacity - output_length` to
3831 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02003832 size_t offset =
3833 generator->ctx.buffer.size - generator->capacity - output_length;
3834 memcpy( output, generator->ctx.buffer.data + offset, output_length );
3835 status = PSA_SUCCESS;
3836 }
3837 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003838#if defined(MBEDTLS_MD_C)
3839 if( PSA_ALG_IS_HKDF( generator->alg ) )
3840 {
3841 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3842 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3843 output, output_length );
3844 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003845 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3846 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003847 {
3848 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
3849 generator->alg, output,
3850 output_length );
3851 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003852 else
3853#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003854 {
3855 return( PSA_ERROR_BAD_STATE );
3856 }
3857
3858exit:
3859 if( status != PSA_SUCCESS )
3860 {
3861 psa_generator_abort( generator );
3862 memset( output, '!', output_length );
3863 }
3864 return( status );
3865}
3866
Gilles Peskine08542d82018-07-19 17:05:42 +02003867#if defined(MBEDTLS_DES_C)
3868static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3869{
3870 if( data_size >= 8 )
3871 mbedtls_des_key_set_parity( data );
3872 if( data_size >= 16 )
3873 mbedtls_des_key_set_parity( data + 8 );
3874 if( data_size >= 24 )
3875 mbedtls_des_key_set_parity( data + 16 );
3876}
3877#endif /* MBEDTLS_DES_C */
3878
Gilles Peskineeab56e42018-07-12 17:12:33 +02003879psa_status_t psa_generator_import_key( psa_key_slot_t key,
3880 psa_key_type_t type,
3881 size_t bits,
3882 psa_crypto_generator_t *generator )
3883{
3884 uint8_t *data = NULL;
3885 size_t bytes = PSA_BITS_TO_BYTES( bits );
3886 psa_status_t status;
3887
3888 if( ! key_type_is_raw_bytes( type ) )
3889 return( PSA_ERROR_INVALID_ARGUMENT );
3890 if( bits % 8 != 0 )
3891 return( PSA_ERROR_INVALID_ARGUMENT );
3892 data = mbedtls_calloc( 1, bytes );
3893 if( data == NULL )
3894 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3895
3896 status = psa_generator_read( generator, data, bytes );
3897 if( status != PSA_SUCCESS )
3898 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003899#if defined(MBEDTLS_DES_C)
3900 if( type == PSA_KEY_TYPE_DES )
3901 psa_des_set_key_parity( data, bytes );
3902#endif /* MBEDTLS_DES_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003903 status = psa_import_key( key, type, data, bytes );
3904
3905exit:
3906 mbedtls_free( data );
3907 return( status );
3908}
3909
3910
3911
3912/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003913/* Key derivation */
3914/****************************************************************/
3915
Gilles Peskinea05219c2018-11-16 16:02:56 +01003916#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02003917/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01003918 * of the HKDF algorithm.
3919 *
3920 * Note that if this function fails, you must call psa_generator_abort()
3921 * to potentially free embedded data structures and wipe confidential data.
3922 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003923static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003924 const uint8_t *secret,
3925 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02003926 psa_algorithm_t hash_alg,
3927 const uint8_t *salt,
3928 size_t salt_length,
3929 const uint8_t *label,
3930 size_t label_length )
3931{
3932 psa_status_t status;
3933 status = psa_hmac_setup_internal( &hkdf->hmac,
3934 salt, salt_length,
Gilles Peskine00709fa2018-08-22 18:25:41 +02003935 PSA_ALG_HMAC_GET_HASH( hash_alg ) );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003936 if( status != PSA_SUCCESS )
3937 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003938 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003939 if( status != PSA_SUCCESS )
3940 return( status );
3941 status = psa_hmac_finish_internal( &hkdf->hmac,
3942 hkdf->prk,
3943 sizeof( hkdf->prk ) );
3944 if( status != PSA_SUCCESS )
3945 return( status );
3946 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
3947 hkdf->block_number = 0;
3948 hkdf->info_length = label_length;
3949 if( label_length != 0 )
3950 {
3951 hkdf->info = mbedtls_calloc( 1, label_length );
3952 if( hkdf->info == NULL )
3953 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3954 memcpy( hkdf->info, label, label_length );
3955 }
3956 return( PSA_SUCCESS );
3957}
Gilles Peskinea05219c2018-11-16 16:02:56 +01003958#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003959
Gilles Peskinea05219c2018-11-16 16:02:56 +01003960#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01003961/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
3962 *
3963 * Note that if this function fails, you must call psa_generator_abort()
3964 * to potentially free embedded data structures and wipe confidential data.
3965 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003966static psa_status_t psa_generator_tls12_prf_setup(
3967 psa_tls12_prf_generator_t *tls12_prf,
3968 const unsigned char *key,
3969 size_t key_len,
3970 psa_algorithm_t hash_alg,
3971 const uint8_t *salt,
3972 size_t salt_length,
3973 const uint8_t *label,
3974 size_t label_length )
3975{
3976 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00003977 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
3978 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003979
3980 tls12_prf->key = mbedtls_calloc( 1, key_len );
3981 if( tls12_prf->key == NULL )
3982 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3983 tls12_prf->key_len = key_len;
3984 memcpy( tls12_prf->key, key, key_len );
3985
Hanno Becker580fba12018-11-13 20:50:45 +00003986 overflow = ( salt_length + label_length < salt_length ) ||
3987 ( salt_length + label_length + hash_length < hash_length );
3988 if( overflow )
3989 return( PSA_ERROR_INVALID_ARGUMENT );
3990
3991 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
3992 if( tls12_prf->Ai_with_seed == NULL )
3993 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3994 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
3995
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003996 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
3997 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00003998 if( label_length != 0 )
3999 {
4000 memcpy( tls12_prf->Ai_with_seed + hash_length,
4001 label, label_length );
4002 }
4003
4004 if( salt_length != 0 )
4005 {
4006 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4007 salt, salt_length );
4008 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004009
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004010 /* The first block gets generated when
4011 * psa_generator_read() is called. */
4012 tls12_prf->block_number = 0;
4013 tls12_prf->offset_in_block = hash_length;
4014
4015 return( PSA_SUCCESS );
4016}
Hanno Becker1aaedc02018-11-16 11:35:34 +00004017
4018/* Set up a TLS-1.2-PSK-to-MS-based generator. */
4019static psa_status_t psa_generator_tls12_psk_to_ms_setup(
4020 psa_tls12_prf_generator_t *tls12_prf,
4021 const unsigned char *psk,
4022 size_t psk_len,
4023 psa_algorithm_t hash_alg,
4024 const uint8_t *salt,
4025 size_t salt_length,
4026 const uint8_t *label,
4027 size_t label_length )
4028{
4029 psa_status_t status;
4030 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4031
4032 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4033 return( PSA_ERROR_INVALID_ARGUMENT );
4034
4035 /* Quoting RFC 4279, Section 2:
4036 *
4037 * The premaster secret is formed as follows: if the PSK is N octets
4038 * long, concatenate a uint16 with the value N, N zero octets, a second
4039 * uint16 with the value N, and the PSK itself.
4040 */
4041
4042 pms[0] = ( psk_len >> 8 ) & 0xff;
4043 pms[1] = ( psk_len >> 0 ) & 0xff;
4044 memset( pms + 2, 0, psk_len );
4045 pms[2 + psk_len + 0] = pms[0];
4046 pms[2 + psk_len + 1] = pms[1];
4047 memcpy( pms + 4 + psk_len, psk, psk_len );
4048
4049 status = psa_generator_tls12_prf_setup( tls12_prf,
4050 pms, 4 + 2 * psk_len,
4051 hash_alg,
4052 salt, salt_length,
4053 label, label_length );
4054
4055 mbedtls_zeroize( pms, sizeof( pms ) );
4056 return( status );
4057}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004058#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004059
Gilles Peskine346797d2018-11-16 16:05:06 +01004060/* Note that if this function fails, you must call psa_generator_abort()
4061 * to potentially free embedded data structures and wipe confidential data.
4062 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004063static psa_status_t psa_key_derivation_internal(
4064 psa_crypto_generator_t *generator,
4065 const uint8_t *secret, size_t secret_length,
4066 psa_algorithm_t alg,
4067 const uint8_t *salt, size_t salt_length,
4068 const uint8_t *label, size_t label_length,
4069 size_t capacity )
4070{
4071 psa_status_t status;
4072 size_t max_capacity;
4073
4074 /* Set generator->alg even on failure so that abort knows what to do. */
4075 generator->alg = alg;
4076
Gilles Peskine751d9652018-09-18 12:05:44 +02004077 if( alg == PSA_ALG_SELECT_RAW )
4078 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01004079 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02004080 if( salt_length != 0 )
4081 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01004082 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02004083 if( label_length != 0 )
4084 return( PSA_ERROR_INVALID_ARGUMENT );
4085 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
4086 if( generator->ctx.buffer.data == NULL )
4087 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4088 memcpy( generator->ctx.buffer.data, secret, secret_length );
4089 generator->ctx.buffer.size = secret_length;
4090 max_capacity = secret_length;
4091 status = PSA_SUCCESS;
4092 }
4093 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004094#if defined(MBEDTLS_MD_C)
4095 if( PSA_ALG_IS_HKDF( alg ) )
4096 {
4097 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4098 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4099 if( hash_size == 0 )
4100 return( PSA_ERROR_NOT_SUPPORTED );
4101 max_capacity = 255 * hash_size;
4102 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
4103 secret, secret_length,
4104 hash_alg,
4105 salt, salt_length,
4106 label, label_length );
4107 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004108 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4109 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4110 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004111 {
4112 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4113 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4114
4115 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4116 if( hash_alg != PSA_ALG_SHA_256 &&
4117 hash_alg != PSA_ALG_SHA_384 )
4118 {
4119 return( PSA_ERROR_NOT_SUPPORTED );
4120 }
4121
4122 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004123
4124 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4125 {
4126 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
4127 secret, secret_length,
4128 hash_alg, salt, salt_length,
4129 label, label_length );
4130 }
4131 else
4132 {
4133 status = psa_generator_tls12_psk_to_ms_setup(
4134 &generator->ctx.tls12_prf,
4135 secret, secret_length,
4136 hash_alg, salt, salt_length,
4137 label, label_length );
4138 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004139 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004140 else
4141#endif
4142 {
4143 return( PSA_ERROR_NOT_SUPPORTED );
4144 }
4145
4146 if( status != PSA_SUCCESS )
4147 return( status );
4148
4149 if( capacity <= max_capacity )
4150 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02004151 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
4152 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004153 else
4154 return( PSA_ERROR_INVALID_ARGUMENT );
4155
4156 return( PSA_SUCCESS );
4157}
4158
Gilles Peskineea0fb492018-07-12 17:17:20 +02004159psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Darryl Green88001362018-07-26 13:59:04 +01004160 psa_key_slot_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004161 psa_algorithm_t alg,
4162 const uint8_t *salt,
4163 size_t salt_length,
4164 const uint8_t *label,
4165 size_t label_length,
4166 size_t capacity )
4167{
4168 key_slot_t *slot;
4169 psa_status_t status;
4170
4171 if( generator->alg != 0 )
4172 return( PSA_ERROR_BAD_STATE );
4173
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004174 /* Make sure that alg is a key derivation algorithm. This prevents
4175 * key selection algorithms, which psa_key_derivation_internal
4176 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004177 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4178 return( PSA_ERROR_INVALID_ARGUMENT );
4179
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004180 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DERIVE, alg );
4181 if( status != PSA_SUCCESS )
4182 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004183
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004184 if( slot->type != PSA_KEY_TYPE_DERIVE )
4185 return( PSA_ERROR_INVALID_ARGUMENT );
4186
4187 status = psa_key_derivation_internal( generator,
4188 slot->data.raw.data,
4189 slot->data.raw.bytes,
4190 alg,
4191 salt, salt_length,
4192 label, label_length,
4193 capacity );
4194 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004195 psa_generator_abort( generator );
4196 return( status );
4197}
4198
4199
4200
4201/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004202/* Key agreement */
4203/****************************************************************/
4204
Gilles Peskinea05219c2018-11-16 16:02:56 +01004205#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004206static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4207 size_t peer_key_length,
4208 const mbedtls_ecp_keypair *our_key,
4209 uint8_t *shared_secret,
4210 size_t shared_secret_size,
4211 size_t *shared_secret_length )
4212{
4213 mbedtls_pk_context pk;
4214 mbedtls_ecp_keypair *their_key = NULL;
4215 mbedtls_ecdh_context ecdh;
4216 int ret;
4217 mbedtls_ecdh_init( &ecdh );
4218 mbedtls_pk_init( &pk );
4219
4220 ret = mbedtls_pk_parse_public_key( &pk, peer_key, peer_key_length );
4221 if( ret != 0 )
4222 goto exit;
Gilles Peskine88714d72018-10-25 23:07:25 +02004223 switch( mbedtls_pk_get_type( &pk ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004224 {
Gilles Peskine88714d72018-10-25 23:07:25 +02004225 case MBEDTLS_PK_ECKEY:
4226 case MBEDTLS_PK_ECKEY_DH:
4227 break;
4228 default:
4229 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
4230 goto exit;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004231 }
4232 their_key = mbedtls_pk_ec( pk );
Gilles Peskineb4086612018-11-14 20:51:23 +01004233 if( their_key->grp.id != our_key->grp.id )
4234 {
4235 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
4236 goto exit;
4237 }
4238
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004239 ret = mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS );
4240 if( ret != 0 )
4241 goto exit;
4242 ret = mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS );
4243 if( ret != 0 )
4244 goto exit;
4245
4246 ret = mbedtls_ecdh_calc_secret( &ecdh,
4247 shared_secret_length,
4248 shared_secret, shared_secret_size,
4249 mbedtls_ctr_drbg_random,
4250 &global_data.ctr_drbg );
4251
4252exit:
4253 mbedtls_pk_free( &pk );
4254 mbedtls_ecdh_free( &ecdh );
4255 return( mbedtls_to_psa_error( ret ) );
4256}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004257#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004258
Gilles Peskine01d718c2018-09-18 12:01:02 +02004259#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4260
Gilles Peskine346797d2018-11-16 16:05:06 +01004261/* Note that if this function fails, you must call psa_generator_abort()
4262 * to potentially free embedded data structures and wipe confidential data.
4263 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004264static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
4265 key_slot_t *private_key,
4266 const uint8_t *peer_key,
4267 size_t peer_key_length,
4268 psa_algorithm_t alg )
4269{
4270 psa_status_t status;
4271 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4272 size_t shared_secret_length = 0;
4273
4274 /* Step 1: run the secret agreement algorithm to generate the shared
4275 * secret. */
4276 switch( PSA_ALG_KEY_AGREEMENT_GET_BASE( alg ) )
4277 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004278#if defined(MBEDTLS_ECDH_C)
4279 case PSA_ALG_ECDH_BASE:
4280 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
4281 return( PSA_ERROR_INVALID_ARGUMENT );
4282 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
4283 private_key->data.ecp,
4284 shared_secret,
4285 sizeof( shared_secret ),
4286 &shared_secret_length );
4287 break;
4288#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004289 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01004290 (void) private_key;
4291 (void) peer_key;
4292 (void) peer_key_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004293 return( PSA_ERROR_NOT_SUPPORTED );
4294 }
4295 if( status != PSA_SUCCESS )
4296 goto exit;
4297
4298 /* Step 2: set up the key derivation to generate key material from
4299 * the shared secret. */
4300 status = psa_key_derivation_internal( generator,
4301 shared_secret, shared_secret_length,
4302 PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ),
4303 NULL, 0, NULL, 0,
4304 PSA_GENERATOR_UNBRIDLED_CAPACITY );
4305exit:
4306 mbedtls_zeroize( shared_secret, shared_secret_length );
4307 return( status );
4308}
4309
4310psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
4311 psa_key_slot_t private_key,
4312 const uint8_t *peer_key,
4313 size_t peer_key_length,
4314 psa_algorithm_t alg )
4315{
4316 key_slot_t *slot;
4317 psa_status_t status;
4318 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
4319 return( PSA_ERROR_INVALID_ARGUMENT );
4320 status = psa_get_key_from_slot( private_key, &slot,
4321 PSA_KEY_USAGE_DERIVE, alg );
4322 if( status != PSA_SUCCESS )
4323 return( status );
Gilles Peskine346797d2018-11-16 16:05:06 +01004324 status = psa_key_agreement_internal( generator,
4325 slot,
4326 peer_key, peer_key_length,
4327 alg );
4328 if( status != PSA_SUCCESS )
4329 psa_generator_abort( generator );
4330 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004331}
4332
4333
4334
4335/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004336/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02004337/****************************************************************/
4338
4339psa_status_t psa_generate_random( uint8_t *output,
4340 size_t output_size )
4341{
itayzafrir0adf0fc2018-09-06 16:24:41 +03004342 int ret;
4343 GUARD_MODULE_INITIALIZED;
4344
4345 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02004346 return( mbedtls_to_psa_error( ret ) );
4347}
4348
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004349#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
avolinski13beb102018-11-20 16:51:49 +02004350
4351/* Support function for error conversion between psa_its error codes to psa crypto */
4352static psa_status_t its_to_psa_error( psa_its_status_t ret )
4353{
4354 switch( ret )
4355 {
4356 case PSA_ITS_SUCCESS:
4357 return( PSA_SUCCESS );
4358
4359 case PSA_ITS_ERROR_KEY_NOT_FOUND:
4360 return( PSA_ERROR_EMPTY_SLOT );
4361
4362 case PSA_ITS_ERROR_STORAGE_FAILURE:
4363 return( PSA_ERROR_STORAGE_FAILURE );
4364
4365 case PSA_ITS_ERROR_INSUFFICIENT_SPACE:
4366 return( PSA_ERROR_INSUFFICIENT_STORAGE );
4367
4368 case PSA_ITS_ERROR_INVALID_KEY:
4369 case PSA_PS_ERROR_OFFSET_INVALID:
4370 case PSA_ITS_ERROR_INCORRECT_SIZE:
4371 case PSA_ITS_ERROR_BAD_POINTER:
4372 return( PSA_ERROR_INVALID_ARGUMENT );
4373
4374 case PSA_ITS_ERROR_FLAGS_NOT_SUPPORTED:
4375 return( PSA_ERROR_NOT_SUPPORTED );
4376
4377 case PSA_ITS_ERROR_WRITE_ONCE:
4378 return( PSA_ERROR_OCCUPIED_SLOT );
4379
4380 default:
4381 return( PSA_ERROR_UNKNOWN_ERROR );
4382 }
4383}
4384
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004385psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
4386 size_t seed_size )
4387{
4388 psa_status_t status;
avolinski13beb102018-11-20 16:51:49 +02004389 psa_its_status_t its_status;
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004390 struct psa_its_info_t p_info;
4391 if( global_data.initialized )
4392 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02004393
4394 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
4395 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
4396 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
4397 return( PSA_ERROR_INVALID_ARGUMENT );
4398
avolinski0d2c2662018-11-21 17:31:07 +02004399 its_status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
avolinski13beb102018-11-20 16:51:49 +02004400 status = its_to_psa_error( its_status );
4401
4402 if( PSA_ITS_ERROR_KEY_NOT_FOUND == its_status ) /* No seed exists */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004403 {
avolinski0d2c2662018-11-21 17:31:07 +02004404 its_status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 );
avolinski13beb102018-11-20 16:51:49 +02004405 status = its_to_psa_error( its_status );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004406 }
avolinski13beb102018-11-20 16:51:49 +02004407 else if( PSA_ITS_SUCCESS == its_status )
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004408 {
4409 /* You should not be here. Seed needs to be injected only once */
4410 status = PSA_ERROR_NOT_PERMITTED;
4411 }
4412 return( status );
4413}
4414#endif
4415
Gilles Peskine05d69892018-06-19 22:00:52 +02004416psa_status_t psa_generate_key( psa_key_slot_t key,
4417 psa_key_type_t type,
4418 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02004419 const void *extra,
4420 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02004421{
Gilles Peskine12313cd2018-06-20 00:20:32 +02004422 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004423 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004424
Gilles Peskine53d991e2018-07-12 01:14:59 +02004425 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004426 return( PSA_ERROR_INVALID_ARGUMENT );
4427
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004428 status = psa_get_empty_key_slot( key, &slot );
4429 if( status != PSA_SUCCESS )
4430 return( status );
4431
Gilles Peskine48c0ea12018-06-21 14:15:31 +02004432 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004433 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004434 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004435 if( status != PSA_SUCCESS )
4436 return( status );
4437 status = psa_generate_random( slot->data.raw.data,
4438 slot->data.raw.bytes );
4439 if( status != PSA_SUCCESS )
4440 {
4441 mbedtls_free( slot->data.raw.data );
4442 return( status );
4443 }
4444#if defined(MBEDTLS_DES_C)
4445 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004446 psa_des_set_key_parity( slot->data.raw.data,
4447 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004448#endif /* MBEDTLS_DES_C */
4449 }
4450 else
4451
4452#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
4453 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
4454 {
4455 mbedtls_rsa_context *rsa;
4456 int ret;
4457 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02004458 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
4459 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01004460 /* Accept only byte-aligned keys, for the same reasons as
4461 * in psa_import_rsa_key(). */
4462 if( bits % 8 != 0 )
4463 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02004464 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004465 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02004466 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004467 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004468 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02004469#if INT_MAX < 0xffffffff
4470 /* Check that the uint32_t value passed by the caller fits
4471 * in the range supported by this implementation. */
4472 if( p->e > INT_MAX )
4473 return( PSA_ERROR_NOT_SUPPORTED );
4474#endif
4475 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004476 }
4477 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
4478 if( rsa == NULL )
4479 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4480 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
4481 ret = mbedtls_rsa_gen_key( rsa,
4482 mbedtls_ctr_drbg_random,
4483 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004484 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02004485 exponent );
4486 if( ret != 0 )
4487 {
4488 mbedtls_rsa_free( rsa );
4489 mbedtls_free( rsa );
4490 return( mbedtls_to_psa_error( ret ) );
4491 }
4492 slot->data.rsa = rsa;
4493 }
4494 else
4495#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
4496
4497#if defined(MBEDTLS_ECP_C)
4498 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
4499 {
4500 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
4501 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
4502 const mbedtls_ecp_curve_info *curve_info =
4503 mbedtls_ecp_curve_info_from_grp_id( grp_id );
4504 mbedtls_ecp_keypair *ecp;
4505 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004506 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004507 return( PSA_ERROR_NOT_SUPPORTED );
4508 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
4509 return( PSA_ERROR_NOT_SUPPORTED );
4510 if( curve_info->bit_size != bits )
4511 return( PSA_ERROR_INVALID_ARGUMENT );
4512 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
4513 if( ecp == NULL )
4514 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4515 mbedtls_ecp_keypair_init( ecp );
4516 ret = mbedtls_ecp_gen_key( grp_id, ecp,
4517 mbedtls_ctr_drbg_random,
4518 &global_data.ctr_drbg );
4519 if( ret != 0 )
4520 {
4521 mbedtls_ecp_keypair_free( ecp );
4522 mbedtls_free( ecp );
4523 return( mbedtls_to_psa_error( ret ) );
4524 }
4525 slot->data.ecp = ecp;
4526 }
4527 else
4528#endif /* MBEDTLS_ECP_C */
4529
4530 return( PSA_ERROR_NOT_SUPPORTED );
4531
4532 slot->type = type;
Darryl Green0c6575a2018-11-07 16:05:30 +00004533
4534#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
4535 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
4536 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01004537 return( psa_save_generated_persistent_key( slot, bits ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004538 }
4539#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
4540
4541 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02004542}
4543
4544
4545/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01004546/* Module setup */
4547/****************************************************************/
4548
Gilles Peskine5e769522018-11-20 21:59:56 +01004549psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
4550 void (* entropy_init )( mbedtls_entropy_context *ctx ),
4551 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
4552{
4553 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4554 return( PSA_ERROR_BAD_STATE );
4555 global_data.entropy_init = entropy_init;
4556 global_data.entropy_free = entropy_free;
4557 return( PSA_SUCCESS );
4558}
4559
Gilles Peskinee59236f2018-01-27 23:32:46 +01004560void mbedtls_psa_crypto_free( void )
4561{
Gilles Peskinec6b69072018-11-20 21:42:52 +01004562 if( global_data.key_slots_initialized )
Darryl Green40225ba2018-11-15 14:48:15 +00004563 {
Gilles Peskined7c75702018-12-03 10:36:46 +01004564 psa_key_slot_t key;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004565 for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
4566 {
Gilles Peskined7c75702018-12-03 10:36:46 +01004567 key_slot_t *slot = &global_data.key_slots[key - 1];
4568 (void) psa_remove_key_data_from_memory( slot );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004569 /* Zeroize the slot to wipe metadata such as policies. */
4570 mbedtls_zeroize( slot, sizeof( *slot ) );
4571 }
Darryl Green40225ba2018-11-15 14:48:15 +00004572 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01004573 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4574 {
4575 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01004576 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004577 }
4578 /* Wipe all remaining data, including configuration.
4579 * In particular, this sets all state indicator to the value
4580 * indicating "uninitialized". */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004581 mbedtls_zeroize( &global_data, sizeof( global_data ) );
4582}
4583
4584psa_status_t psa_crypto_init( void )
4585{
4586 int ret;
4587 const unsigned char drbg_seed[] = "PSA";
4588
Gilles Peskinec6b69072018-11-20 21:42:52 +01004589 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004590 if( global_data.initialized != 0 )
4591 return( PSA_SUCCESS );
4592
Gilles Peskine5e769522018-11-20 21:59:56 +01004593 /* Set default configuration if
4594 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
4595 if( global_data.entropy_init == NULL )
4596 global_data.entropy_init = mbedtls_entropy_init;
4597 if( global_data.entropy_free == NULL )
4598 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004599
Gilles Peskinec6b69072018-11-20 21:42:52 +01004600 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01004601 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004602 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004603 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004604 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
4605 mbedtls_entropy_func,
4606 &global_data.entropy,
4607 drbg_seed, sizeof( drbg_seed ) - 1 );
4608 if( ret != 0 )
4609 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004610 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004611
Gilles Peskinec6b69072018-11-20 21:42:52 +01004612 /* Initialize the key slots. Zero-initialization has made all key
4613 * slots empty, so there is nothing to do. In a future version we will
4614 * load data from storage. */
4615 global_data.key_slots_initialized = 1;
4616
4617 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01004618 global_data.initialized = 1;
4619
Gilles Peskinee59236f2018-01-27 23:32:46 +01004620exit:
4621 if( ret != 0 )
4622 mbedtls_psa_crypto_free( );
4623 return( mbedtls_to_psa_error( ret ) );
4624}
4625
4626#endif /* MBEDTLS_PSA_CRYPTO_C */