blob: 2030315dd60a4378ad1e10beb35775997e4322ff [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 Peskine2f9c4dc2018-01-28 13:16:24 +010046#include <stdlib.h>
47#include <string.h>
48#if defined(MBEDTLS_PLATFORM_C)
49#include "mbedtls/platform.h"
50#else
51#define mbedtls_calloc calloc
52#define mbedtls_free free
53#endif
54
Gilles Peskinea5905292018-02-07 20:59:33 +010055#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020056#include "mbedtls/asn1.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020057#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010058#include "mbedtls/blowfish.h"
59#include "mbedtls/camellia.h"
60#include "mbedtls/cipher.h"
61#include "mbedtls/ccm.h"
62#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010063#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020065#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010066#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010067#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010068#include "mbedtls/error.h"
69#include "mbedtls/gcm.h"
70#include "mbedtls/md2.h"
71#include "mbedtls/md4.h"
72#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010073#include "mbedtls/md.h"
74#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010075#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010076#include "mbedtls/pk_internal.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010077#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010078#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010079#include "mbedtls/sha1.h"
80#include "mbedtls/sha256.h"
81#include "mbedtls/sha512.h"
82#include "mbedtls/xtea.h"
83
Gilles Peskinee59236f2018-01-27 23:32:46 +010084
85
Gilles Peskine996deb12018-08-01 15:45:45 +020086#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
87
Gilles Peskinee59236f2018-01-27 23:32:46 +010088/* Implementation that should never be optimized out by the compiler */
89static void mbedtls_zeroize( void *v, size_t n )
90{
91 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
92}
93
Gilles Peskine9ef733f2018-02-07 21:05:37 +010094/* constant-time buffer comparison */
95static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
96{
97 size_t i;
98 unsigned char diff = 0;
99
100 for( i = 0; i < n; i++ )
101 diff |= a[i] ^ b[i];
102
103 return( diff );
104}
105
106
107
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100108/****************************************************************/
109/* Global data, support functions and library management */
110/****************************************************************/
111
112/* Number of key slots (plus one because 0 is not used).
113 * The value is a compile-time constant for now, for simplicity. */
Gilles Peskine828ed142018-06-18 23:25:51 +0200114#define PSA_KEY_SLOT_COUNT 32
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100115
Gilles Peskine2d277862018-06-18 15:41:12 +0200116typedef struct
117{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100118 psa_key_type_t type;
mohammad16038cc1cee2018-03-28 01:21:33 +0300119 psa_key_policy_t policy;
mohammad1603804cd712018-03-20 22:44:08 +0200120 psa_key_lifetime_t lifetime;
Gilles Peskine2d277862018-06-18 15:41:12 +0200121 union
122 {
123 struct raw_data
124 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100125 uint8_t *data;
126 size_t bytes;
127 } raw;
Gilles Peskine969ac722018-01-28 18:16:59 +0100128#if defined(MBEDTLS_RSA_C)
129 mbedtls_rsa_context *rsa;
130#endif /* MBEDTLS_RSA_C */
131#if defined(MBEDTLS_ECP_C)
132 mbedtls_ecp_keypair *ecp;
133#endif /* MBEDTLS_ECP_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100134 } data;
135} key_slot_t;
136
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200137static int key_type_is_raw_bytes( psa_key_type_t type )
138{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200139 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200140}
141
Gilles Peskine2d277862018-06-18 15:41:12 +0200142typedef struct
143{
Gilles Peskinee59236f2018-01-27 23:32:46 +0100144 int initialized;
145 mbedtls_entropy_context entropy;
146 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskine828ed142018-06-18 23:25:51 +0200147 key_slot_t key_slots[PSA_KEY_SLOT_COUNT];
Gilles Peskinee59236f2018-01-27 23:32:46 +0100148} psa_global_data_t;
149
150static psa_global_data_t global_data;
151
itayzafrir0adf0fc2018-09-06 16:24:41 +0300152#define GUARD_MODULE_INITIALIZED \
153 if( global_data.initialized == 0 ) \
154 return( PSA_ERROR_BAD_STATE );
155
Gilles Peskinee59236f2018-01-27 23:32:46 +0100156static psa_status_t mbedtls_to_psa_error( int ret )
157{
Gilles Peskinea5905292018-02-07 20:59:33 +0100158 /* If there's both a high-level code and low-level code, dispatch on
159 * the high-level code. */
160 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100161 {
162 case 0:
163 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100164
165 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
166 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
167 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
168 return( PSA_ERROR_NOT_SUPPORTED );
169 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
170 return( PSA_ERROR_HARDWARE_FAILURE );
171
172 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
173 return( PSA_ERROR_HARDWARE_FAILURE );
174
Gilles Peskine9a944802018-06-21 09:35:35 +0200175 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
176 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
177 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
178 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
179 case MBEDTLS_ERR_ASN1_INVALID_DATA:
180 return( PSA_ERROR_INVALID_ARGUMENT );
181 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
182 return( PSA_ERROR_INSUFFICIENT_MEMORY );
183 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
184 return( PSA_ERROR_BUFFER_TOO_SMALL );
185
Gilles Peskinea5905292018-02-07 20:59:33 +0100186 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
187 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
188 return( PSA_ERROR_NOT_SUPPORTED );
189 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
190 return( PSA_ERROR_HARDWARE_FAILURE );
191
192 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
193 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
194 return( PSA_ERROR_NOT_SUPPORTED );
195 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
196 return( PSA_ERROR_HARDWARE_FAILURE );
197
198 case MBEDTLS_ERR_CCM_BAD_INPUT:
199 return( PSA_ERROR_INVALID_ARGUMENT );
200 case MBEDTLS_ERR_CCM_AUTH_FAILED:
201 return( PSA_ERROR_INVALID_SIGNATURE );
202 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
203 return( PSA_ERROR_HARDWARE_FAILURE );
204
205 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
206 return( PSA_ERROR_NOT_SUPPORTED );
207 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
208 return( PSA_ERROR_INVALID_ARGUMENT );
209 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
210 return( PSA_ERROR_INSUFFICIENT_MEMORY );
211 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
212 return( PSA_ERROR_INVALID_PADDING );
213 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
214 return( PSA_ERROR_BAD_STATE );
215 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
216 return( PSA_ERROR_INVALID_SIGNATURE );
217 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
218 return( PSA_ERROR_TAMPERING_DETECTED );
219 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
220 return( PSA_ERROR_HARDWARE_FAILURE );
221
222 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
223 return( PSA_ERROR_HARDWARE_FAILURE );
224
225 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
226 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
227 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
228 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
229 return( PSA_ERROR_NOT_SUPPORTED );
230 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
231 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
232
233 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
234 return( PSA_ERROR_NOT_SUPPORTED );
235 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
236 return( PSA_ERROR_HARDWARE_FAILURE );
237
Gilles Peskinee59236f2018-01-27 23:32:46 +0100238 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
239 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
240 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
241 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100242
243 case MBEDTLS_ERR_GCM_AUTH_FAILED:
244 return( PSA_ERROR_INVALID_SIGNATURE );
245 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200246 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100247 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
248 return( PSA_ERROR_HARDWARE_FAILURE );
249
250 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
251 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
252 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
253 return( PSA_ERROR_HARDWARE_FAILURE );
254
255 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
256 return( PSA_ERROR_NOT_SUPPORTED );
257 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
258 return( PSA_ERROR_INVALID_ARGUMENT );
259 case MBEDTLS_ERR_MD_ALLOC_FAILED:
260 return( PSA_ERROR_INSUFFICIENT_MEMORY );
261 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
262 return( PSA_ERROR_STORAGE_FAILURE );
263 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
264 return( PSA_ERROR_HARDWARE_FAILURE );
265
Gilles Peskinef76aa772018-10-29 19:24:33 +0100266 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
267 return( PSA_ERROR_STORAGE_FAILURE );
268 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
269 return( PSA_ERROR_INVALID_ARGUMENT );
270 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
271 return( PSA_ERROR_INVALID_ARGUMENT );
272 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
273 return( PSA_ERROR_BUFFER_TOO_SMALL );
274 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
275 return( PSA_ERROR_INVALID_ARGUMENT );
276 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
277 return( PSA_ERROR_INVALID_ARGUMENT );
278 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
279 return( PSA_ERROR_INVALID_ARGUMENT );
280 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
281 return( PSA_ERROR_INSUFFICIENT_MEMORY );
282
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100283 case MBEDTLS_ERR_PK_ALLOC_FAILED:
284 return( PSA_ERROR_INSUFFICIENT_MEMORY );
285 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
286 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
287 return( PSA_ERROR_INVALID_ARGUMENT );
288 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100289 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100290 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
291 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
292 return( PSA_ERROR_INVALID_ARGUMENT );
293 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
294 return( PSA_ERROR_NOT_SUPPORTED );
295 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
296 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
297 return( PSA_ERROR_NOT_PERMITTED );
298 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
299 return( PSA_ERROR_INVALID_ARGUMENT );
300 case MBEDTLS_ERR_PK_INVALID_ALG:
301 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
302 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
303 return( PSA_ERROR_NOT_SUPPORTED );
304 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
305 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100306 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
307 return( PSA_ERROR_HARDWARE_FAILURE );
308
309 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
310 return( PSA_ERROR_HARDWARE_FAILURE );
311
312 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
313 return( PSA_ERROR_INVALID_ARGUMENT );
314 case MBEDTLS_ERR_RSA_INVALID_PADDING:
315 return( PSA_ERROR_INVALID_PADDING );
316 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
317 return( PSA_ERROR_HARDWARE_FAILURE );
318 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
319 return( PSA_ERROR_INVALID_ARGUMENT );
320 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
321 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
322 return( PSA_ERROR_TAMPERING_DETECTED );
323 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
324 return( PSA_ERROR_INVALID_SIGNATURE );
325 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
326 return( PSA_ERROR_BUFFER_TOO_SMALL );
327 case MBEDTLS_ERR_RSA_RNG_FAILED:
328 return( PSA_ERROR_INSUFFICIENT_MEMORY );
329 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
330 return( PSA_ERROR_NOT_SUPPORTED );
331 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
332 return( PSA_ERROR_HARDWARE_FAILURE );
333
334 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
335 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
336 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
337 return( PSA_ERROR_HARDWARE_FAILURE );
338
339 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
340 return( PSA_ERROR_INVALID_ARGUMENT );
341 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
342 return( PSA_ERROR_HARDWARE_FAILURE );
343
itayzafrir5c753392018-05-08 11:18:38 +0300344 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300345 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300346 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300347 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
348 return( PSA_ERROR_BUFFER_TOO_SMALL );
349 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
350 return( PSA_ERROR_NOT_SUPPORTED );
351 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
352 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
353 return( PSA_ERROR_INVALID_SIGNATURE );
354 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
355 return( PSA_ERROR_INSUFFICIENT_MEMORY );
356 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
357 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300358
Gilles Peskinee59236f2018-01-27 23:32:46 +0100359 default:
360 return( PSA_ERROR_UNKNOWN_ERROR );
361 }
362}
363
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200364/* Retrieve a key slot, occupied or not. */
365static psa_status_t psa_get_key_slot( psa_key_slot_t key,
366 key_slot_t **p_slot )
367{
itayzafrir90d8c7a2018-09-12 11:44:52 +0300368 GUARD_MODULE_INITIALIZED;
369
Gilles Peskine996deb12018-08-01 15:45:45 +0200370 /* 0 is not a valid slot number under any circumstance. This
371 * implementation provides slots number 1 to N where N is the
372 * number of available slots. */
373 if( key == 0 || key > ARRAY_LENGTH( global_data.key_slots ) )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200374 return( PSA_ERROR_INVALID_ARGUMENT );
375
Gilles Peskine996deb12018-08-01 15:45:45 +0200376 *p_slot = &global_data.key_slots[key - 1];
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200377 return( PSA_SUCCESS );
378}
379
380/* Retrieve an empty key slot (slot with no key data, but possibly
381 * with some metadata such as a policy). */
382static psa_status_t psa_get_empty_key_slot( psa_key_slot_t key,
383 key_slot_t **p_slot )
384{
385 psa_status_t status;
386 key_slot_t *slot = NULL;
387
388 *p_slot = NULL;
389
390 status = psa_get_key_slot( key, &slot );
391 if( status != PSA_SUCCESS )
392 return( status );
393
394 if( slot->type != PSA_KEY_TYPE_NONE )
395 return( PSA_ERROR_OCCUPIED_SLOT );
396
397 *p_slot = slot;
398 return( status );
399}
400
Jaeden Amerob4fa8c92018-07-11 15:57:44 +0100401/** Retrieve a slot which must contain a key. The key must have allow all the
402 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
403 * operations with this algorithm. */
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200404static psa_status_t psa_get_key_from_slot( psa_key_slot_t key,
405 key_slot_t **p_slot,
406 psa_key_usage_t usage,
407 psa_algorithm_t alg )
408{
409 psa_status_t status;
410 key_slot_t *slot = NULL;
411
412 *p_slot = NULL;
413
414 status = psa_get_key_slot( key, &slot );
415 if( status != PSA_SUCCESS )
416 return( status );
417 if( slot->type == PSA_KEY_TYPE_NONE )
418 return( PSA_ERROR_EMPTY_SLOT );
419
420 /* Enforce that usage policy for the key slot contains all the flags
421 * required by the usage parameter. There is one exception: public
422 * keys can always be exported, so we treat public key objects as
423 * if they had the export flag. */
424 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
425 usage &= ~PSA_KEY_USAGE_EXPORT;
426 if( ( slot->policy.usage & usage ) != usage )
427 return( PSA_ERROR_NOT_PERMITTED );
428 if( alg != 0 && ( alg != slot->policy.alg ) )
429 return( PSA_ERROR_NOT_PERMITTED );
430
431 *p_slot = slot;
432 return( PSA_SUCCESS );
433}
434
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200435
436
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100437/****************************************************************/
438/* Key management */
439/****************************************************************/
440
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100441#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200442static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
443{
444 switch( grpid )
445 {
446 case MBEDTLS_ECP_DP_SECP192R1:
447 return( PSA_ECC_CURVE_SECP192R1 );
448 case MBEDTLS_ECP_DP_SECP224R1:
449 return( PSA_ECC_CURVE_SECP224R1 );
450 case MBEDTLS_ECP_DP_SECP256R1:
451 return( PSA_ECC_CURVE_SECP256R1 );
452 case MBEDTLS_ECP_DP_SECP384R1:
453 return( PSA_ECC_CURVE_SECP384R1 );
454 case MBEDTLS_ECP_DP_SECP521R1:
455 return( PSA_ECC_CURVE_SECP521R1 );
456 case MBEDTLS_ECP_DP_BP256R1:
457 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
458 case MBEDTLS_ECP_DP_BP384R1:
459 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
460 case MBEDTLS_ECP_DP_BP512R1:
461 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
462 case MBEDTLS_ECP_DP_CURVE25519:
463 return( PSA_ECC_CURVE_CURVE25519 );
464 case MBEDTLS_ECP_DP_SECP192K1:
465 return( PSA_ECC_CURVE_SECP192K1 );
466 case MBEDTLS_ECP_DP_SECP224K1:
467 return( PSA_ECC_CURVE_SECP224K1 );
468 case MBEDTLS_ECP_DP_SECP256K1:
469 return( PSA_ECC_CURVE_SECP256K1 );
470 case MBEDTLS_ECP_DP_CURVE448:
471 return( PSA_ECC_CURVE_CURVE448 );
472 default:
473 return( 0 );
474 }
475}
476
Gilles Peskine12313cd2018-06-20 00:20:32 +0200477static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
478{
479 switch( curve )
480 {
481 case PSA_ECC_CURVE_SECP192R1:
482 return( MBEDTLS_ECP_DP_SECP192R1 );
483 case PSA_ECC_CURVE_SECP224R1:
484 return( MBEDTLS_ECP_DP_SECP224R1 );
485 case PSA_ECC_CURVE_SECP256R1:
486 return( MBEDTLS_ECP_DP_SECP256R1 );
487 case PSA_ECC_CURVE_SECP384R1:
488 return( MBEDTLS_ECP_DP_SECP384R1 );
489 case PSA_ECC_CURVE_SECP521R1:
490 return( MBEDTLS_ECP_DP_SECP521R1 );
491 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
492 return( MBEDTLS_ECP_DP_BP256R1 );
493 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
494 return( MBEDTLS_ECP_DP_BP384R1 );
495 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
496 return( MBEDTLS_ECP_DP_BP512R1 );
497 case PSA_ECC_CURVE_CURVE25519:
498 return( MBEDTLS_ECP_DP_CURVE25519 );
499 case PSA_ECC_CURVE_SECP192K1:
500 return( MBEDTLS_ECP_DP_SECP192K1 );
501 case PSA_ECC_CURVE_SECP224K1:
502 return( MBEDTLS_ECP_DP_SECP224K1 );
503 case PSA_ECC_CURVE_SECP256K1:
504 return( MBEDTLS_ECP_DP_SECP256K1 );
505 case PSA_ECC_CURVE_CURVE448:
506 return( MBEDTLS_ECP_DP_CURVE448 );
507 default:
508 return( MBEDTLS_ECP_DP_NONE );
509 }
510}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100511#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200512
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200513static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
514 size_t bits,
515 struct raw_data *raw )
516{
517 /* Check that the bit size is acceptable for the key type */
518 switch( type )
519 {
520 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200521 if( bits == 0 )
522 {
523 raw->bytes = 0;
524 raw->data = NULL;
525 return( PSA_SUCCESS );
526 }
527 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200528#if defined(MBEDTLS_MD_C)
529 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200530#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200531 case PSA_KEY_TYPE_DERIVE:
532 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200533#if defined(MBEDTLS_AES_C)
534 case PSA_KEY_TYPE_AES:
535 if( bits != 128 && bits != 192 && bits != 256 )
536 return( PSA_ERROR_INVALID_ARGUMENT );
537 break;
538#endif
539#if defined(MBEDTLS_CAMELLIA_C)
540 case PSA_KEY_TYPE_CAMELLIA:
541 if( bits != 128 && bits != 192 && bits != 256 )
542 return( PSA_ERROR_INVALID_ARGUMENT );
543 break;
544#endif
545#if defined(MBEDTLS_DES_C)
546 case PSA_KEY_TYPE_DES:
547 if( bits != 64 && bits != 128 && bits != 192 )
548 return( PSA_ERROR_INVALID_ARGUMENT );
549 break;
550#endif
551#if defined(MBEDTLS_ARC4_C)
552 case PSA_KEY_TYPE_ARC4:
553 if( bits < 8 || bits > 2048 )
554 return( PSA_ERROR_INVALID_ARGUMENT );
555 break;
556#endif
557 default:
558 return( PSA_ERROR_NOT_SUPPORTED );
559 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200560 if( bits % 8 != 0 )
561 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200562
563 /* Allocate memory for the key */
564 raw->bytes = PSA_BITS_TO_BYTES( bits );
565 raw->data = mbedtls_calloc( 1, raw->bytes );
566 if( raw->data == NULL )
567 {
568 raw->bytes = 0;
569 return( PSA_ERROR_INSUFFICIENT_MEMORY );
570 }
571 return( PSA_SUCCESS );
572}
573
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200574#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100575/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
576 * that are not a multiple of 8) well. For example, there is only
577 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
578 * way to return the exact bit size of a key.
579 * To keep things simple, reject non-byte-aligned key sizes. */
580static psa_status_t psa_check_rsa_key_byte_aligned(
581 const mbedtls_rsa_context *rsa )
582{
583 mbedtls_mpi n;
584 psa_status_t status;
585 mbedtls_mpi_init( &n );
586 status = mbedtls_to_psa_error(
587 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
588 if( status == PSA_SUCCESS )
589 {
590 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
591 status = PSA_ERROR_NOT_SUPPORTED;
592 }
593 mbedtls_mpi_free( &n );
594 return( status );
595}
596
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200597static psa_status_t psa_import_rsa_key( mbedtls_pk_context *pk,
598 mbedtls_rsa_context **p_rsa )
599{
600 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_RSA )
601 return( PSA_ERROR_INVALID_ARGUMENT );
602 else
603 {
604 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *pk );
Gilles Peskineaac64a22018-11-12 18:37:42 +0100605 /* The size of an RSA key doesn't have to be a multiple of 8.
606 * Mbed TLS supports non-byte-aligned key sizes, but not well.
607 * For example, mbedtls_rsa_get_len() returns the key size in
608 * bytes, not in bits. */
609 size_t bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
Gilles Peskine86a440b2018-11-12 18:39:40 +0100610 psa_status_t status;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200611 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
612 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +0100613 status = psa_check_rsa_key_byte_aligned( rsa );
614 if( status != PSA_SUCCESS )
615 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200616 *p_rsa = rsa;
617 return( PSA_SUCCESS );
618 }
619}
620#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
621
622#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinef76aa772018-10-29 19:24:33 +0100623/* Import an elliptic curve parsed by the mbedtls pk module. */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200624static psa_status_t psa_import_ecp_key( psa_ecc_curve_t expected_curve,
625 mbedtls_pk_context *pk,
626 mbedtls_ecp_keypair **p_ecp )
627{
628 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_ECKEY )
629 return( PSA_ERROR_INVALID_ARGUMENT );
630 else
631 {
632 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( *pk );
633 psa_ecc_curve_t actual_curve = mbedtls_ecc_group_to_psa( ecp->grp.id );
634 if( actual_curve != expected_curve )
635 return( PSA_ERROR_INVALID_ARGUMENT );
636 *p_ecp = ecp;
637 return( PSA_SUCCESS );
638 }
639}
640#endif /* defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C) */
641
Gilles Peskinef76aa772018-10-29 19:24:33 +0100642#if defined(MBEDTLS_ECP_C)
643/* Import a private key given as a byte string which is the private value
644 * in big-endian order. */
645static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
646 const uint8_t *data,
647 size_t data_length,
648 mbedtls_ecp_keypair **p_ecp )
649{
650 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
651 mbedtls_ecp_keypair *ecp = NULL;
652 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
653
654 *p_ecp = NULL;
655 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
656 if( ecp == NULL )
657 return( PSA_ERROR_INSUFFICIENT_MEMORY );
658
659 /* Load the group. */
660 status = mbedtls_to_psa_error(
661 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
662 if( status != PSA_SUCCESS )
663 goto exit;
664 /* Load the secret value. */
665 status = mbedtls_to_psa_error(
666 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
667 if( status != PSA_SUCCESS )
668 goto exit;
669 /* Validate the private key. */
670 status = mbedtls_to_psa_error(
671 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
672 if( status != PSA_SUCCESS )
673 goto exit;
674 /* Calculate the public key from the private key. */
675 status = mbedtls_to_psa_error(
676 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
677 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
678 if( status != PSA_SUCCESS )
679 goto exit;
680
681 *p_ecp = ecp;
682 return( PSA_SUCCESS );
683
684exit:
685 if( ecp != NULL )
686 {
687 mbedtls_ecp_keypair_free( ecp );
688 mbedtls_free( ecp );
689 }
690 return( status );
691}
692#endif /* defined(MBEDTLS_ECP_C) */
693
Gilles Peskine2d277862018-06-18 15:41:12 +0200694psa_status_t psa_import_key( psa_key_slot_t key,
695 psa_key_type_t type,
696 const uint8_t *data,
697 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100698{
699 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200700 psa_status_t status = PSA_SUCCESS;
701 status = psa_get_empty_key_slot( key, &slot );
702 if( status != PSA_SUCCESS )
703 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100704
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200705 if( key_type_is_raw_bytes( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100706 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100707 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100708 if( data_length > SIZE_MAX / 8 )
709 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200710 status = prepare_raw_data_slot( type,
711 PSA_BYTES_TO_BITS( data_length ),
712 &slot->data.raw );
713 if( status != PSA_SUCCESS )
714 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200715 if( data_length != 0 )
716 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100717 }
718 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100719#if defined(MBEDTLS_ECP_C)
720 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) )
721 {
722 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( type ),
723 data, data_length,
724 &slot->data.ecp );
725 if( status != PSA_SUCCESS )
726 return( status );
727 }
728 else
729#endif /* MBEDTLS_ECP_C */
Gilles Peskine969ac722018-01-28 18:16:59 +0100730#if defined(MBEDTLS_PK_PARSE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200731 if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100732 {
733 int ret;
Gilles Peskine969ac722018-01-28 18:16:59 +0100734 mbedtls_pk_context pk;
735 mbedtls_pk_init( &pk );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200736
737 /* Parse the data. */
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100738 if( PSA_KEY_TYPE_IS_KEYPAIR( type ) )
739 ret = mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 );
740 else
741 ret = mbedtls_pk_parse_public_key( &pk, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100742 if( ret != 0 )
743 return( mbedtls_to_psa_error( ret ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200744
745 /* We have something that the pkparse module recognizes.
746 * If it has the expected type and passes any type-specific
747 * checks, store it. */
Gilles Peskine969ac722018-01-28 18:16:59 +0100748#if defined(MBEDTLS_RSA_C)
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200749 if( PSA_KEY_TYPE_IS_RSA( type ) )
750 status = psa_import_rsa_key( &pk, &slot->data.rsa );
751 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100752#endif /* MBEDTLS_RSA_C */
753#if defined(MBEDTLS_ECP_C)
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200754 if( PSA_KEY_TYPE_IS_ECC( type ) )
755 status = psa_import_ecp_key( PSA_KEY_TYPE_GET_CURVE( type ),
756 &pk, &slot->data.ecp );
757 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100758#endif /* MBEDTLS_ECP_C */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200759 {
760 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinec648d692018-06-28 08:46:13 +0200761 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200762
Gilles Peskinec648d692018-06-28 08:46:13 +0200763 /* Free the content of the pk object only on error. On success,
764 * the content of the object has been stored in the slot. */
765 if( status != PSA_SUCCESS )
766 {
767 mbedtls_pk_free( &pk );
768 return( status );
Gilles Peskine969ac722018-01-28 18:16:59 +0100769 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100770 }
771 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100772#endif /* defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100773 {
774 return( PSA_ERROR_NOT_SUPPORTED );
775 }
776
777 slot->type = type;
778 return( PSA_SUCCESS );
779}
780
Gilles Peskine2d277862018-06-18 15:41:12 +0200781psa_status_t psa_destroy_key( psa_key_slot_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100782{
783 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200784 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100785
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200786 status = psa_get_key_slot( key, &slot );
787 if( status != PSA_SUCCESS )
788 return( status );
789
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100790 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskine154bd952018-04-19 08:38:16 +0200791 {
792 /* No key material to clean, but do zeroize the slot below to wipe
793 * metadata such as policies. */
794 }
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200795 else if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100796 {
797 mbedtls_free( slot->data.raw.data );
798 }
799 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100800#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200801 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100802 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100803 mbedtls_rsa_free( slot->data.rsa );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100804 mbedtls_free( slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100805 }
806 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100807#endif /* defined(MBEDTLS_RSA_C) */
808#if defined(MBEDTLS_ECP_C)
809 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
810 {
811 mbedtls_ecp_keypair_free( slot->data.ecp );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100812 mbedtls_free( slot->data.ecp );
Gilles Peskine969ac722018-01-28 18:16:59 +0100813 }
814 else
815#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100816 {
817 /* Shouldn't happen: the key type is not any type that we
Gilles Peskine6d912132018-03-07 16:41:37 +0100818 * put in. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100819 return( PSA_ERROR_TAMPERING_DETECTED );
820 }
821
822 mbedtls_zeroize( slot, sizeof( *slot ) );
823 return( PSA_SUCCESS );
824}
825
Gilles Peskineb870b182018-07-06 16:02:09 +0200826/* Return the size of the key in the given slot, in bits. */
827static size_t psa_get_key_bits( const key_slot_t *slot )
828{
829 if( key_type_is_raw_bytes( slot->type ) )
830 return( slot->data.raw.bytes * 8 );
831#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200832 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +0100833 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +0200834#endif /* defined(MBEDTLS_RSA_C) */
835#if defined(MBEDTLS_ECP_C)
836 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
837 return( slot->data.ecp->grp.pbits );
838#endif /* defined(MBEDTLS_ECP_C) */
839 /* Shouldn't happen except on an empty slot. */
840 return( 0 );
841}
842
Gilles Peskine2d277862018-06-18 15:41:12 +0200843psa_status_t psa_get_key_information( psa_key_slot_t key,
844 psa_key_type_t *type,
845 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100846{
847 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200848 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100849
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100850 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200851 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100852 if( bits != NULL )
853 *bits = 0;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200854 status = psa_get_key_slot( key, &slot );
855 if( status != PSA_SUCCESS )
856 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +0200857
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100858 if( slot->type == PSA_KEY_TYPE_NONE )
859 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200860 if( type != NULL )
861 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +0200862 if( bits != NULL )
863 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100864 return( PSA_SUCCESS );
865}
866
Gilles Peskine2d277862018-06-18 15:41:12 +0200867static psa_status_t psa_internal_export_key( psa_key_slot_t key,
868 uint8_t *data,
869 size_t data_size,
870 size_t *data_length,
871 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100872{
873 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200874 psa_status_t status;
875 /* Exporting a public key doesn't require a usage flag. If we're
876 * called by psa_export_public_key(), don't require the EXPORT flag.
877 * If we're called by psa_export_key(), do require the EXPORT flag;
878 * if the key turns out to be public key object, psa_get_key_from_slot()
879 * will ignore this flag. */
880 psa_key_usage_t usage = export_public_key ? 0 : PSA_KEY_USAGE_EXPORT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100881
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100882 /* Set the key to empty now, so that even when there are errors, we always
883 * set data_length to a value between 0 and data_size. On error, setting
884 * the key to empty is a good choice because an empty key representation is
885 * unlikely to be accepted anywhere. */
886 *data_length = 0;
887
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200888 status = psa_get_key_from_slot( key, &slot, usage, 0 );
889 if( status != PSA_SUCCESS )
890 return( status );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200891 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300892 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +0300893
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200894 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100895 {
896 if( slot->data.raw.bytes > data_size )
897 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +0100898 if( data_size != 0 )
899 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200900 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +0100901 memset( data + slot->data.raw.bytes, 0,
902 data_size - slot->data.raw.bytes );
903 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100904 *data_length = slot->data.raw.bytes;
905 return( PSA_SUCCESS );
906 }
Gilles Peskine188c71e2018-10-29 19:26:02 +0100907#if defined(MBEDTLS_ECP_C)
908 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
909 {
910 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( slot ) );
911 if( bytes > data_size )
912 return( PSA_ERROR_BUFFER_TOO_SMALL );
913 status = mbedtls_to_psa_error(
914 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
915 if( status != PSA_SUCCESS )
916 return( status );
917 memset( data + bytes, 0, data_size - bytes );
918 *data_length = bytes;
919 return( PSA_SUCCESS );
920 }
921#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100922 else
Moran Peker17e36e12018-05-02 12:55:20 +0300923 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100924#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200925 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +0300926 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +0100927 {
Moran Pekera998bc62018-04-16 18:16:20 +0300928 mbedtls_pk_context pk;
929 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +0200930 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300931 {
Darryl Green9e2d7a02018-07-24 16:33:30 +0100932#if defined(MBEDTLS_RSA_C)
933 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +0300934 pk.pk_info = &mbedtls_rsa_info;
935 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +0100936#else
937 return( PSA_ERROR_NOT_SUPPORTED );
938#endif
Moran Pekera998bc62018-04-16 18:16:20 +0300939 }
940 else
941 {
Darryl Green9e2d7a02018-07-24 16:33:30 +0100942#if defined(MBEDTLS_ECP_C)
943 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +0300944 pk.pk_info = &mbedtls_eckey_info;
945 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +0100946#else
947 return( PSA_ERROR_NOT_SUPPORTED );
948#endif
Moran Pekera998bc62018-04-16 18:16:20 +0300949 }
Moran Pekerd7326592018-05-29 16:56:39 +0300950 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300951 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +0300952 else
953 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +0300954 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200955 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200956 /* If data_size is 0 then data may be NULL and then the
957 * call to memset would have undefined behavior. */
958 if( data_size != 0 )
959 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +0300960 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200961 }
Gilles Peskine0e231582018-06-20 00:11:07 +0200962 /* The mbedtls_pk_xxx functions write to the end of the buffer.
963 * Move the data to the beginning and erase remaining data
964 * at the original location. */
965 if( 2 * (size_t) ret <= data_size )
966 {
967 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200968 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200969 }
970 else if( (size_t) ret < data_size )
971 {
972 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200973 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200974 }
Moran Pekera998bc62018-04-16 18:16:20 +0300975 *data_length = ret;
976 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +0100977 }
978 else
Gilles Peskine6d912132018-03-07 16:41:37 +0100979#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +0300980 {
981 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +0200982 it is valid for a special-purpose implementation to omit
983 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +0300984 return( PSA_ERROR_NOT_SUPPORTED );
985 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100986 }
987}
988
Gilles Peskine2d277862018-06-18 15:41:12 +0200989psa_status_t psa_export_key( psa_key_slot_t key,
990 uint8_t *data,
991 size_t data_size,
992 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +0300993{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200994 return( psa_internal_export_key( key, data, data_size,
995 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100996}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100997
Gilles Peskine2d277862018-06-18 15:41:12 +0200998psa_status_t psa_export_public_key( psa_key_slot_t key,
999 uint8_t *data,
1000 size_t data_size,
1001 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001002{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001003 return( psa_internal_export_key( key, data, data_size,
1004 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001005}
1006
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001007
1008
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001009/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001010/* Message digests */
1011/****************************************************************/
1012
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001013static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001014{
1015 switch( alg )
1016 {
1017#if defined(MBEDTLS_MD2_C)
1018 case PSA_ALG_MD2:
1019 return( &mbedtls_md2_info );
1020#endif
1021#if defined(MBEDTLS_MD4_C)
1022 case PSA_ALG_MD4:
1023 return( &mbedtls_md4_info );
1024#endif
1025#if defined(MBEDTLS_MD5_C)
1026 case PSA_ALG_MD5:
1027 return( &mbedtls_md5_info );
1028#endif
1029#if defined(MBEDTLS_RIPEMD160_C)
1030 case PSA_ALG_RIPEMD160:
1031 return( &mbedtls_ripemd160_info );
1032#endif
1033#if defined(MBEDTLS_SHA1_C)
1034 case PSA_ALG_SHA_1:
1035 return( &mbedtls_sha1_info );
1036#endif
1037#if defined(MBEDTLS_SHA256_C)
1038 case PSA_ALG_SHA_224:
1039 return( &mbedtls_sha224_info );
1040 case PSA_ALG_SHA_256:
1041 return( &mbedtls_sha256_info );
1042#endif
1043#if defined(MBEDTLS_SHA512_C)
1044 case PSA_ALG_SHA_384:
1045 return( &mbedtls_sha384_info );
1046 case PSA_ALG_SHA_512:
1047 return( &mbedtls_sha512_info );
1048#endif
1049 default:
1050 return( NULL );
1051 }
1052}
1053
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001054psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1055{
1056 switch( operation->alg )
1057 {
Gilles Peskine81736312018-06-26 15:04:31 +02001058 case 0:
1059 /* The object has (apparently) been initialized but it is not
1060 * in use. It's ok to call abort on such an object, and there's
1061 * nothing to do. */
1062 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001063#if defined(MBEDTLS_MD2_C)
1064 case PSA_ALG_MD2:
1065 mbedtls_md2_free( &operation->ctx.md2 );
1066 break;
1067#endif
1068#if defined(MBEDTLS_MD4_C)
1069 case PSA_ALG_MD4:
1070 mbedtls_md4_free( &operation->ctx.md4 );
1071 break;
1072#endif
1073#if defined(MBEDTLS_MD5_C)
1074 case PSA_ALG_MD5:
1075 mbedtls_md5_free( &operation->ctx.md5 );
1076 break;
1077#endif
1078#if defined(MBEDTLS_RIPEMD160_C)
1079 case PSA_ALG_RIPEMD160:
1080 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1081 break;
1082#endif
1083#if defined(MBEDTLS_SHA1_C)
1084 case PSA_ALG_SHA_1:
1085 mbedtls_sha1_free( &operation->ctx.sha1 );
1086 break;
1087#endif
1088#if defined(MBEDTLS_SHA256_C)
1089 case PSA_ALG_SHA_224:
1090 case PSA_ALG_SHA_256:
1091 mbedtls_sha256_free( &operation->ctx.sha256 );
1092 break;
1093#endif
1094#if defined(MBEDTLS_SHA512_C)
1095 case PSA_ALG_SHA_384:
1096 case PSA_ALG_SHA_512:
1097 mbedtls_sha512_free( &operation->ctx.sha512 );
1098 break;
1099#endif
1100 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001101 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001102 }
1103 operation->alg = 0;
1104 return( PSA_SUCCESS );
1105}
1106
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001107psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001108 psa_algorithm_t alg )
1109{
1110 int ret;
1111 operation->alg = 0;
1112 switch( alg )
1113 {
1114#if defined(MBEDTLS_MD2_C)
1115 case PSA_ALG_MD2:
1116 mbedtls_md2_init( &operation->ctx.md2 );
1117 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1118 break;
1119#endif
1120#if defined(MBEDTLS_MD4_C)
1121 case PSA_ALG_MD4:
1122 mbedtls_md4_init( &operation->ctx.md4 );
1123 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1124 break;
1125#endif
1126#if defined(MBEDTLS_MD5_C)
1127 case PSA_ALG_MD5:
1128 mbedtls_md5_init( &operation->ctx.md5 );
1129 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1130 break;
1131#endif
1132#if defined(MBEDTLS_RIPEMD160_C)
1133 case PSA_ALG_RIPEMD160:
1134 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1135 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1136 break;
1137#endif
1138#if defined(MBEDTLS_SHA1_C)
1139 case PSA_ALG_SHA_1:
1140 mbedtls_sha1_init( &operation->ctx.sha1 );
1141 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1142 break;
1143#endif
1144#if defined(MBEDTLS_SHA256_C)
1145 case PSA_ALG_SHA_224:
1146 mbedtls_sha256_init( &operation->ctx.sha256 );
1147 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1148 break;
1149 case PSA_ALG_SHA_256:
1150 mbedtls_sha256_init( &operation->ctx.sha256 );
1151 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1152 break;
1153#endif
1154#if defined(MBEDTLS_SHA512_C)
1155 case PSA_ALG_SHA_384:
1156 mbedtls_sha512_init( &operation->ctx.sha512 );
1157 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1158 break;
1159 case PSA_ALG_SHA_512:
1160 mbedtls_sha512_init( &operation->ctx.sha512 );
1161 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1162 break;
1163#endif
1164 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001165 return( PSA_ALG_IS_HASH( alg ) ?
1166 PSA_ERROR_NOT_SUPPORTED :
1167 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001168 }
1169 if( ret == 0 )
1170 operation->alg = alg;
1171 else
1172 psa_hash_abort( operation );
1173 return( mbedtls_to_psa_error( ret ) );
1174}
1175
1176psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1177 const uint8_t *input,
1178 size_t input_length )
1179{
1180 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001181
1182 /* Don't require hash implementations to behave correctly on a
1183 * zero-length input, which may have an invalid pointer. */
1184 if( input_length == 0 )
1185 return( PSA_SUCCESS );
1186
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001187 switch( operation->alg )
1188 {
1189#if defined(MBEDTLS_MD2_C)
1190 case PSA_ALG_MD2:
1191 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1192 input, input_length );
1193 break;
1194#endif
1195#if defined(MBEDTLS_MD4_C)
1196 case PSA_ALG_MD4:
1197 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1198 input, input_length );
1199 break;
1200#endif
1201#if defined(MBEDTLS_MD5_C)
1202 case PSA_ALG_MD5:
1203 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1204 input, input_length );
1205 break;
1206#endif
1207#if defined(MBEDTLS_RIPEMD160_C)
1208 case PSA_ALG_RIPEMD160:
1209 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1210 input, input_length );
1211 break;
1212#endif
1213#if defined(MBEDTLS_SHA1_C)
1214 case PSA_ALG_SHA_1:
1215 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1216 input, input_length );
1217 break;
1218#endif
1219#if defined(MBEDTLS_SHA256_C)
1220 case PSA_ALG_SHA_224:
1221 case PSA_ALG_SHA_256:
1222 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1223 input, input_length );
1224 break;
1225#endif
1226#if defined(MBEDTLS_SHA512_C)
1227 case PSA_ALG_SHA_384:
1228 case PSA_ALG_SHA_512:
1229 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1230 input, input_length );
1231 break;
1232#endif
1233 default:
1234 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1235 break;
1236 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001237
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001238 if( ret != 0 )
1239 psa_hash_abort( operation );
1240 return( mbedtls_to_psa_error( ret ) );
1241}
1242
1243psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1244 uint8_t *hash,
1245 size_t hash_size,
1246 size_t *hash_length )
1247{
itayzafrir40835d42018-08-02 13:14:17 +03001248 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001249 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001250 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001251
1252 /* Fill the output buffer with something that isn't a valid hash
1253 * (barring an attack on the hash and deliberately-crafted input),
1254 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001255 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001256 /* If hash_size is 0 then hash may be NULL and then the
1257 * call to memset would have undefined behavior. */
1258 if( hash_size != 0 )
1259 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001260
1261 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001262 {
1263 status = PSA_ERROR_BUFFER_TOO_SMALL;
1264 goto exit;
1265 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001266
1267 switch( operation->alg )
1268 {
1269#if defined(MBEDTLS_MD2_C)
1270 case PSA_ALG_MD2:
1271 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1272 break;
1273#endif
1274#if defined(MBEDTLS_MD4_C)
1275 case PSA_ALG_MD4:
1276 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1277 break;
1278#endif
1279#if defined(MBEDTLS_MD5_C)
1280 case PSA_ALG_MD5:
1281 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1282 break;
1283#endif
1284#if defined(MBEDTLS_RIPEMD160_C)
1285 case PSA_ALG_RIPEMD160:
1286 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1287 break;
1288#endif
1289#if defined(MBEDTLS_SHA1_C)
1290 case PSA_ALG_SHA_1:
1291 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1292 break;
1293#endif
1294#if defined(MBEDTLS_SHA256_C)
1295 case PSA_ALG_SHA_224:
1296 case PSA_ALG_SHA_256:
1297 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1298 break;
1299#endif
1300#if defined(MBEDTLS_SHA512_C)
1301 case PSA_ALG_SHA_384:
1302 case PSA_ALG_SHA_512:
1303 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1304 break;
1305#endif
1306 default:
1307 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1308 break;
1309 }
itayzafrir40835d42018-08-02 13:14:17 +03001310 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001311
itayzafrir40835d42018-08-02 13:14:17 +03001312exit:
1313 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001314 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001315 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001316 return( psa_hash_abort( operation ) );
1317 }
1318 else
1319 {
1320 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001321 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001322 }
1323}
1324
Gilles Peskine2d277862018-06-18 15:41:12 +02001325psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1326 const uint8_t *hash,
1327 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001328{
1329 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1330 size_t actual_hash_length;
1331 psa_status_t status = psa_hash_finish( operation,
1332 actual_hash, sizeof( actual_hash ),
1333 &actual_hash_length );
1334 if( status != PSA_SUCCESS )
1335 return( status );
1336 if( actual_hash_length != hash_length )
1337 return( PSA_ERROR_INVALID_SIGNATURE );
1338 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1339 return( PSA_ERROR_INVALID_SIGNATURE );
1340 return( PSA_SUCCESS );
1341}
1342
1343
1344
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001345/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001346/* MAC */
1347/****************************************************************/
1348
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001349static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001350 psa_algorithm_t alg,
1351 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001352 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001353 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001354{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001355 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001356 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001357
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001358 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001359 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001360
Gilles Peskine8c9def32018-02-08 10:02:12 +01001361 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1362 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001363 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001364 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001365 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001366 mode = MBEDTLS_MODE_STREAM;
1367 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001368 case PSA_ALG_CTR:
1369 mode = MBEDTLS_MODE_CTR;
1370 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001371 case PSA_ALG_CFB:
1372 mode = MBEDTLS_MODE_CFB;
1373 break;
1374 case PSA_ALG_OFB:
1375 mode = MBEDTLS_MODE_OFB;
1376 break;
1377 case PSA_ALG_CBC_NO_PADDING:
1378 mode = MBEDTLS_MODE_CBC;
1379 break;
1380 case PSA_ALG_CBC_PKCS7:
1381 mode = MBEDTLS_MODE_CBC;
1382 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001383 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001384 mode = MBEDTLS_MODE_CCM;
1385 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001386 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001387 mode = MBEDTLS_MODE_GCM;
1388 break;
1389 default:
1390 return( NULL );
1391 }
1392 }
1393 else if( alg == PSA_ALG_CMAC )
1394 mode = MBEDTLS_MODE_ECB;
1395 else if( alg == PSA_ALG_GMAC )
1396 mode = MBEDTLS_MODE_GCM;
1397 else
1398 return( NULL );
1399
1400 switch( key_type )
1401 {
1402 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001403 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001404 break;
1405 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001406 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1407 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001408 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001409 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001410 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001411 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001412 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1413 * but two-key Triple-DES is functionally three-key Triple-DES
1414 * with K1=K3, so that's how we present it to mbedtls. */
1415 if( key_bits == 128 )
1416 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001417 break;
1418 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001419 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001420 break;
1421 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001422 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001423 break;
1424 default:
1425 return( NULL );
1426 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001427 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001428 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001429
Jaeden Amero23bbb752018-06-26 14:16:54 +01001430 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1431 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001432}
1433
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001434static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001435{
Gilles Peskine2d277862018-06-18 15:41:12 +02001436 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001437 {
1438 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001439 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001440 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001441 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001442 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001443 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001444 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001445 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001446 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001447 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001448 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001449 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001450 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001451 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001452 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001453 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001454 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001455 return( 128 );
1456 default:
1457 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001458 }
1459}
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001460
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001461/* Initialize the MAC operation structure. Once this function has been
1462 * called, psa_mac_abort can run and will do the right thing. */
1463static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1464 psa_algorithm_t alg )
1465{
1466 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1467
1468 operation->alg = alg;
1469 operation->key_set = 0;
1470 operation->iv_set = 0;
1471 operation->iv_required = 0;
1472 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001473 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001474
1475#if defined(MBEDTLS_CMAC_C)
1476 if( alg == PSA_ALG_CMAC )
1477 {
1478 operation->iv_required = 0;
1479 mbedtls_cipher_init( &operation->ctx.cmac );
1480 status = PSA_SUCCESS;
1481 }
1482 else
1483#endif /* MBEDTLS_CMAC_C */
1484#if defined(MBEDTLS_MD_C)
1485 if( PSA_ALG_IS_HMAC( operation->alg ) )
1486 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001487 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1488 operation->ctx.hmac.hash_ctx.alg = 0;
1489 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001490 }
1491 else
1492#endif /* MBEDTLS_MD_C */
1493 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001494 if( ! PSA_ALG_IS_MAC( alg ) )
1495 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001496 }
1497
1498 if( status != PSA_SUCCESS )
1499 memset( operation, 0, sizeof( *operation ) );
1500 return( status );
1501}
1502
Gilles Peskine01126fa2018-07-12 17:04:55 +02001503#if defined(MBEDTLS_MD_C)
1504static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1505{
1506 mbedtls_zeroize( hmac->opad, sizeof( hmac->opad ) );
1507 return( psa_hash_abort( &hmac->hash_ctx ) );
1508}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01001509
1510static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
1511{
1512 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
1513 memset( hmac, 0, sizeof( *hmac ) );
1514}
Gilles Peskine01126fa2018-07-12 17:04:55 +02001515#endif /* MBEDTLS_MD_C */
1516
Gilles Peskine8c9def32018-02-08 10:02:12 +01001517psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1518{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001519 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001520 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001521 /* The object has (apparently) been initialized but it is not
1522 * in use. It's ok to call abort on such an object, and there's
1523 * nothing to do. */
1524 return( PSA_SUCCESS );
1525 }
1526 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001527#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001528 if( operation->alg == PSA_ALG_CMAC )
1529 {
1530 mbedtls_cipher_free( &operation->ctx.cmac );
1531 }
1532 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001533#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001534#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001535 if( PSA_ALG_IS_HMAC( operation->alg ) )
1536 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001537 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001538 }
1539 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001540#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001541 {
1542 /* Sanity check (shouldn't happen: operation->alg should
1543 * always have been initialized to a valid value). */
1544 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001545 }
Moran Peker41deec42018-04-04 15:43:05 +03001546
Gilles Peskine8c9def32018-02-08 10:02:12 +01001547 operation->alg = 0;
1548 operation->key_set = 0;
1549 operation->iv_set = 0;
1550 operation->iv_required = 0;
1551 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001552 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001553
Gilles Peskine8c9def32018-02-08 10:02:12 +01001554 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001555
1556bad_state:
1557 /* If abort is called on an uninitialized object, we can't trust
1558 * anything. Wipe the object in case it contains confidential data.
1559 * This may result in a memory leak if a pointer gets overwritten,
1560 * but it's too late to do anything about this. */
1561 memset( operation, 0, sizeof( *operation ) );
1562 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001563}
1564
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001565#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001566static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001567 size_t key_bits,
1568 key_slot_t *slot,
1569 const mbedtls_cipher_info_t *cipher_info )
1570{
1571 int ret;
1572
1573 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001574
1575 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1576 if( ret != 0 )
1577 return( ret );
1578
1579 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1580 slot->data.raw.data,
1581 key_bits );
1582 return( ret );
1583}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001584#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001585
Gilles Peskine248051a2018-06-20 16:09:38 +02001586#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001587static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1588 const uint8_t *key,
1589 size_t key_length,
1590 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001591{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001592 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001593 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001594 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001595 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001596 psa_status_t status;
1597
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001598 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1599 * overflow below. This should never trigger if the hash algorithm
1600 * is implemented correctly. */
1601 /* The size checks against the ipad and opad buffers cannot be written
1602 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1603 * because that triggers -Wlogical-op on GCC 7.3. */
1604 if( block_size > sizeof( ipad ) )
1605 return( PSA_ERROR_NOT_SUPPORTED );
1606 if( block_size > sizeof( hmac->opad ) )
1607 return( PSA_ERROR_NOT_SUPPORTED );
1608 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001609 return( PSA_ERROR_NOT_SUPPORTED );
1610
Gilles Peskined223b522018-06-11 18:12:58 +02001611 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001612 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001613 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001614 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001615 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001616 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001617 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001618 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001619 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001620 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001621 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001622 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001623 }
Gilles Peskine96889972018-07-12 17:07:03 +02001624 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1625 * but it is permitted. It is common when HMAC is used in HKDF, for
1626 * example. Don't call `memcpy` in the 0-length because `key` could be
1627 * an invalid pointer which would make the behavior undefined. */
1628 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001629 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001630
Gilles Peskined223b522018-06-11 18:12:58 +02001631 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1632 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001633 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001634 ipad[i] ^= 0x36;
1635 memset( ipad + key_length, 0x36, block_size - key_length );
1636
1637 /* Copy the key material from ipad to opad, flipping the requisite bits,
1638 * and filling the rest of opad with the requisite constant. */
1639 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001640 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1641 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001642
Gilles Peskine01126fa2018-07-12 17:04:55 +02001643 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001644 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001645 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001646
Gilles Peskine01126fa2018-07-12 17:04:55 +02001647 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001648
1649cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001650 mbedtls_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001651
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001652 return( status );
1653}
Gilles Peskine248051a2018-06-20 16:09:38 +02001654#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001655
Gilles Peskine89167cb2018-07-08 20:12:23 +02001656static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
1657 psa_key_slot_t key,
1658 psa_algorithm_t alg,
1659 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001660{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001661 psa_status_t status;
1662 key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001663 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001664 psa_key_usage_t usage =
1665 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02001666 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02001667 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001668
Gilles Peskined911eb72018-08-14 15:18:45 +02001669 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001670 if( status != PSA_SUCCESS )
1671 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001672 if( is_sign )
1673 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001674
Gilles Peskine89167cb2018-07-08 20:12:23 +02001675 status = psa_get_key_from_slot( key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001676 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001677 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001678 key_bits = psa_get_key_bits( slot );
1679
Gilles Peskine8c9def32018-02-08 10:02:12 +01001680#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001681 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001682 {
1683 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02001684 mbedtls_cipher_info_from_psa( full_length_alg,
1685 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001686 int ret;
1687 if( cipher_info == NULL )
1688 {
1689 status = PSA_ERROR_NOT_SUPPORTED;
1690 goto exit;
1691 }
1692 operation->mac_size = cipher_info->block_size;
1693 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
1694 status = mbedtls_to_psa_error( ret );
1695 }
1696 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001697#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001698#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001699 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001700 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02001701 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001702 if( hash_alg == 0 )
1703 {
1704 status = PSA_ERROR_NOT_SUPPORTED;
1705 goto exit;
1706 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001707
1708 operation->mac_size = PSA_HASH_SIZE( hash_alg );
1709 /* Sanity check. This shouldn't fail on a valid configuration. */
1710 if( operation->mac_size == 0 ||
1711 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
1712 {
1713 status = PSA_ERROR_NOT_SUPPORTED;
1714 goto exit;
1715 }
1716
Gilles Peskine01126fa2018-07-12 17:04:55 +02001717 if( slot->type != PSA_KEY_TYPE_HMAC )
1718 {
1719 status = PSA_ERROR_INVALID_ARGUMENT;
1720 goto exit;
1721 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001722
Gilles Peskine01126fa2018-07-12 17:04:55 +02001723 status = psa_hmac_setup_internal( &operation->ctx.hmac,
1724 slot->data.raw.data,
1725 slot->data.raw.bytes,
1726 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001727 }
1728 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001729#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001730 {
1731 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001732 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001733
Gilles Peskined911eb72018-08-14 15:18:45 +02001734 if( truncated == 0 )
1735 {
1736 /* The "normal" case: untruncated algorithm. Nothing to do. */
1737 }
1738 else if( truncated < 4 )
1739 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02001740 /* A very short MAC is too short for security since it can be
1741 * brute-forced. Ancient protocols with 32-bit MACs do exist,
1742 * so we make this our minimum, even though 32 bits is still
1743 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02001744 status = PSA_ERROR_NOT_SUPPORTED;
1745 }
1746 else if( truncated > operation->mac_size )
1747 {
1748 /* It's impossible to "truncate" to a larger length. */
1749 status = PSA_ERROR_INVALID_ARGUMENT;
1750 }
1751 else
1752 operation->mac_size = truncated;
1753
Gilles Peskinefbfac682018-07-08 20:51:54 +02001754exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001755 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001756 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001757 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001758 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001759 else
1760 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001761 operation->key_set = 1;
1762 }
1763 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001764}
1765
Gilles Peskine89167cb2018-07-08 20:12:23 +02001766psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
1767 psa_key_slot_t key,
1768 psa_algorithm_t alg )
1769{
1770 return( psa_mac_setup( operation, key, alg, 1 ) );
1771}
1772
1773psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
1774 psa_key_slot_t key,
1775 psa_algorithm_t alg )
1776{
1777 return( psa_mac_setup( operation, key, alg, 0 ) );
1778}
1779
Gilles Peskine8c9def32018-02-08 10:02:12 +01001780psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1781 const uint8_t *input,
1782 size_t input_length )
1783{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001784 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001785 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001786 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001787 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001788 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001789 operation->has_input = 1;
1790
Gilles Peskine8c9def32018-02-08 10:02:12 +01001791#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001792 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001793 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001794 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1795 input, input_length );
1796 status = mbedtls_to_psa_error( ret );
1797 }
1798 else
1799#endif /* MBEDTLS_CMAC_C */
1800#if defined(MBEDTLS_MD_C)
1801 if( PSA_ALG_IS_HMAC( operation->alg ) )
1802 {
1803 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
1804 input_length );
1805 }
1806 else
1807#endif /* MBEDTLS_MD_C */
1808 {
1809 /* This shouldn't happen if `operation` was initialized by
1810 * a setup function. */
1811 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001812 }
1813
Gilles Peskinefbfac682018-07-08 20:51:54 +02001814cleanup:
1815 if( status != PSA_SUCCESS )
1816 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001817 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001818}
1819
Gilles Peskine01126fa2018-07-12 17:04:55 +02001820#if defined(MBEDTLS_MD_C)
1821static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
1822 uint8_t *mac,
1823 size_t mac_size )
1824{
1825 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
1826 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
1827 size_t hash_size = 0;
1828 size_t block_size = psa_get_hash_block_size( hash_alg );
1829 psa_status_t status;
1830
Gilles Peskine01126fa2018-07-12 17:04:55 +02001831 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1832 if( status != PSA_SUCCESS )
1833 return( status );
1834 /* From here on, tmp needs to be wiped. */
1835
1836 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
1837 if( status != PSA_SUCCESS )
1838 goto exit;
1839
1840 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
1841 if( status != PSA_SUCCESS )
1842 goto exit;
1843
1844 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
1845 if( status != PSA_SUCCESS )
1846 goto exit;
1847
Gilles Peskined911eb72018-08-14 15:18:45 +02001848 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1849 if( status != PSA_SUCCESS )
1850 goto exit;
1851
1852 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001853
1854exit:
1855 mbedtls_zeroize( tmp, hash_size );
1856 return( status );
1857}
1858#endif /* MBEDTLS_MD_C */
1859
mohammad16036df908f2018-04-02 08:34:15 -07001860static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02001861 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001862 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001863{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02001864 if( ! operation->key_set )
1865 return( PSA_ERROR_BAD_STATE );
1866 if( operation->iv_required && ! operation->iv_set )
1867 return( PSA_ERROR_BAD_STATE );
1868
Gilles Peskine8c9def32018-02-08 10:02:12 +01001869 if( mac_size < operation->mac_size )
1870 return( PSA_ERROR_BUFFER_TOO_SMALL );
1871
Gilles Peskine8c9def32018-02-08 10:02:12 +01001872#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001873 if( operation->alg == PSA_ALG_CMAC )
1874 {
Gilles Peskined911eb72018-08-14 15:18:45 +02001875 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
1876 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
1877 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02001878 memcpy( mac, tmp, operation->mac_size );
Gilles Peskined911eb72018-08-14 15:18:45 +02001879 mbedtls_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001880 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001881 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02001882 else
1883#endif /* MBEDTLS_CMAC_C */
1884#if defined(MBEDTLS_MD_C)
1885 if( PSA_ALG_IS_HMAC( operation->alg ) )
1886 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001887 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02001888 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001889 }
1890 else
1891#endif /* MBEDTLS_MD_C */
1892 {
1893 /* This shouldn't happen if `operation` was initialized by
1894 * a setup function. */
1895 return( PSA_ERROR_BAD_STATE );
1896 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001897}
1898
Gilles Peskineacd4be32018-07-08 19:56:25 +02001899psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
1900 uint8_t *mac,
1901 size_t mac_size,
1902 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07001903{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001904 psa_status_t status;
1905
1906 /* Fill the output buffer with something that isn't a valid mac
1907 * (barring an attack on the mac and deliberately-crafted input),
1908 * in case the caller doesn't check the return status properly. */
1909 *mac_length = mac_size;
1910 /* If mac_size is 0 then mac may be NULL and then the
1911 * call to memset would have undefined behavior. */
1912 if( mac_size != 0 )
1913 memset( mac, '!', mac_size );
1914
Gilles Peskine89167cb2018-07-08 20:12:23 +02001915 if( ! operation->is_sign )
1916 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001917 status = PSA_ERROR_BAD_STATE;
1918 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001919 }
mohammad16036df908f2018-04-02 08:34:15 -07001920
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001921 status = psa_mac_finish_internal( operation, mac, mac_size );
1922
1923cleanup:
1924 if( status == PSA_SUCCESS )
1925 {
1926 status = psa_mac_abort( operation );
1927 if( status == PSA_SUCCESS )
1928 *mac_length = operation->mac_size;
1929 else
1930 memset( mac, '!', mac_size );
1931 }
1932 else
1933 psa_mac_abort( operation );
1934 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07001935}
1936
Gilles Peskineacd4be32018-07-08 19:56:25 +02001937psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
1938 const uint8_t *mac,
1939 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001940{
Gilles Peskine828ed142018-06-18 23:25:51 +02001941 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07001942 psa_status_t status;
1943
Gilles Peskine89167cb2018-07-08 20:12:23 +02001944 if( operation->is_sign )
1945 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001946 status = PSA_ERROR_BAD_STATE;
1947 goto cleanup;
1948 }
1949 if( operation->mac_size != mac_length )
1950 {
1951 status = PSA_ERROR_INVALID_SIGNATURE;
1952 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001953 }
mohammad16036df908f2018-04-02 08:34:15 -07001954
1955 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001956 actual_mac, sizeof( actual_mac ) );
1957
1958 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
1959 status = PSA_ERROR_INVALID_SIGNATURE;
1960
1961cleanup:
1962 if( status == PSA_SUCCESS )
1963 status = psa_mac_abort( operation );
1964 else
1965 psa_mac_abort( operation );
1966
Gilles Peskine99b7d6b2018-08-21 14:56:19 +02001967 mbedtls_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02001968
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001969 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001970}
1971
1972
Gilles Peskine20035e32018-02-03 22:44:14 +01001973
Gilles Peskine20035e32018-02-03 22:44:14 +01001974/****************************************************************/
1975/* Asymmetric cryptography */
1976/****************************************************************/
1977
Gilles Peskine2b450e32018-06-27 15:42:46 +02001978#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001979/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001980 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001981static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
1982 size_t hash_length,
1983 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001984{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001985 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001986 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001987 *md_alg = mbedtls_md_get_type( md_info );
1988
1989 /* The Mbed TLS RSA module uses an unsigned int for hash length
1990 * parameters. Validate that it fits so that we don't risk an
1991 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001992#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001993 if( hash_length > UINT_MAX )
1994 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001995#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001996
1997#if defined(MBEDTLS_PKCS1_V15)
1998 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
1999 * must be correct. */
2000 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2001 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002002 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002003 if( md_info == NULL )
2004 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002005 if( mbedtls_md_get_size( md_info ) != hash_length )
2006 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002007 }
2008#endif /* MBEDTLS_PKCS1_V15 */
2009
2010#if defined(MBEDTLS_PKCS1_V21)
2011 /* PSS requires a hash internally. */
2012 if( PSA_ALG_IS_RSA_PSS( alg ) )
2013 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002014 if( md_info == NULL )
2015 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002016 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002017#endif /* MBEDTLS_PKCS1_V21 */
2018
Gilles Peskine61b91d42018-06-08 16:09:36 +02002019 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002020}
2021
Gilles Peskine2b450e32018-06-27 15:42:46 +02002022static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2023 psa_algorithm_t alg,
2024 const uint8_t *hash,
2025 size_t hash_length,
2026 uint8_t *signature,
2027 size_t signature_size,
2028 size_t *signature_length )
2029{
2030 psa_status_t status;
2031 int ret;
2032 mbedtls_md_type_t md_alg;
2033
2034 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2035 if( status != PSA_SUCCESS )
2036 return( status );
2037
Gilles Peskine630a18a2018-06-29 17:49:35 +02002038 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002039 return( PSA_ERROR_BUFFER_TOO_SMALL );
2040
2041#if defined(MBEDTLS_PKCS1_V15)
2042 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2043 {
2044 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2045 MBEDTLS_MD_NONE );
2046 ret = mbedtls_rsa_pkcs1_sign( rsa,
2047 mbedtls_ctr_drbg_random,
2048 &global_data.ctr_drbg,
2049 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002050 md_alg,
2051 (unsigned int) hash_length,
2052 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002053 signature );
2054 }
2055 else
2056#endif /* MBEDTLS_PKCS1_V15 */
2057#if defined(MBEDTLS_PKCS1_V21)
2058 if( PSA_ALG_IS_RSA_PSS( alg ) )
2059 {
2060 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2061 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2062 mbedtls_ctr_drbg_random,
2063 &global_data.ctr_drbg,
2064 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002065 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002066 (unsigned int) hash_length,
2067 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002068 signature );
2069 }
2070 else
2071#endif /* MBEDTLS_PKCS1_V21 */
2072 {
2073 return( PSA_ERROR_INVALID_ARGUMENT );
2074 }
2075
2076 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002077 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002078 return( mbedtls_to_psa_error( ret ) );
2079}
2080
2081static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2082 psa_algorithm_t alg,
2083 const uint8_t *hash,
2084 size_t hash_length,
2085 const uint8_t *signature,
2086 size_t signature_length )
2087{
2088 psa_status_t status;
2089 int ret;
2090 mbedtls_md_type_t md_alg;
2091
2092 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2093 if( status != PSA_SUCCESS )
2094 return( status );
2095
Gilles Peskine630a18a2018-06-29 17:49:35 +02002096 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002097 return( PSA_ERROR_BUFFER_TOO_SMALL );
2098
2099#if defined(MBEDTLS_PKCS1_V15)
2100 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2101 {
2102 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2103 MBEDTLS_MD_NONE );
2104 ret = mbedtls_rsa_pkcs1_verify( rsa,
2105 mbedtls_ctr_drbg_random,
2106 &global_data.ctr_drbg,
2107 MBEDTLS_RSA_PUBLIC,
2108 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002109 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002110 hash,
2111 signature );
2112 }
2113 else
2114#endif /* MBEDTLS_PKCS1_V15 */
2115#if defined(MBEDTLS_PKCS1_V21)
2116 if( PSA_ALG_IS_RSA_PSS( alg ) )
2117 {
2118 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2119 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2120 mbedtls_ctr_drbg_random,
2121 &global_data.ctr_drbg,
2122 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002123 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002124 (unsigned int) hash_length,
2125 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002126 signature );
2127 }
2128 else
2129#endif /* MBEDTLS_PKCS1_V21 */
2130 {
2131 return( PSA_ERROR_INVALID_ARGUMENT );
2132 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002133
2134 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2135 * the rest of the signature is invalid". This has little use in
2136 * practice and PSA doesn't report this distinction. */
2137 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2138 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002139 return( mbedtls_to_psa_error( ret ) );
2140}
2141#endif /* MBEDTLS_RSA_C */
2142
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002143#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002144/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2145 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2146 * (even though these functions don't modify it). */
2147static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2148 psa_algorithm_t alg,
2149 const uint8_t *hash,
2150 size_t hash_length,
2151 uint8_t *signature,
2152 size_t signature_size,
2153 size_t *signature_length )
2154{
2155 int ret;
2156 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002157 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002158 mbedtls_mpi_init( &r );
2159 mbedtls_mpi_init( &s );
2160
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002161 if( signature_size < 2 * curve_bytes )
2162 {
2163 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2164 goto cleanup;
2165 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002166
2167 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2168 {
2169 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2170 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2171 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2172 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2173 hash, hash_length,
2174 md_alg ) );
2175 }
2176 else
2177 {
2178 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2179 hash, hash_length,
2180 mbedtls_ctr_drbg_random,
2181 &global_data.ctr_drbg ) );
2182 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002183
2184 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2185 signature,
2186 curve_bytes ) );
2187 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2188 signature + curve_bytes,
2189 curve_bytes ) );
2190
2191cleanup:
2192 mbedtls_mpi_free( &r );
2193 mbedtls_mpi_free( &s );
2194 if( ret == 0 )
2195 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002196 return( mbedtls_to_psa_error( ret ) );
2197}
2198
2199static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2200 const uint8_t *hash,
2201 size_t hash_length,
2202 const uint8_t *signature,
2203 size_t signature_length )
2204{
2205 int ret;
2206 mbedtls_mpi r, s;
2207 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2208 mbedtls_mpi_init( &r );
2209 mbedtls_mpi_init( &s );
2210
2211 if( signature_length != 2 * curve_bytes )
2212 return( PSA_ERROR_INVALID_SIGNATURE );
2213
2214 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2215 signature,
2216 curve_bytes ) );
2217 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2218 signature + curve_bytes,
2219 curve_bytes ) );
2220
2221 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2222 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002223
2224cleanup:
2225 mbedtls_mpi_free( &r );
2226 mbedtls_mpi_free( &s );
2227 return( mbedtls_to_psa_error( ret ) );
2228}
2229#endif /* MBEDTLS_ECDSA_C */
2230
Gilles Peskine61b91d42018-06-08 16:09:36 +02002231psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
2232 psa_algorithm_t alg,
2233 const uint8_t *hash,
2234 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002235 uint8_t *signature,
2236 size_t signature_size,
2237 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002238{
2239 key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002240 psa_status_t status;
2241
2242 *signature_length = signature_size;
2243
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002244 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_SIGN, alg );
2245 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002246 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002247 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002248 {
2249 status = PSA_ERROR_INVALID_ARGUMENT;
2250 goto exit;
2251 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002252
Gilles Peskine20035e32018-02-03 22:44:14 +01002253#if defined(MBEDTLS_RSA_C)
2254 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2255 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002256 status = psa_rsa_sign( slot->data.rsa,
2257 alg,
2258 hash, hash_length,
2259 signature, signature_size,
2260 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002261 }
2262 else
2263#endif /* defined(MBEDTLS_RSA_C) */
2264#if defined(MBEDTLS_ECP_C)
2265 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2266 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002267#if defined(MBEDTLS_ECDSA_C)
2268 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002269 status = psa_ecdsa_sign( slot->data.ecp,
2270 alg,
2271 hash, hash_length,
2272 signature, signature_size,
2273 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002274 else
2275#endif /* defined(MBEDTLS_ECDSA_C) */
2276 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002277 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002278 }
itayzafrir5c753392018-05-08 11:18:38 +03002279 }
2280 else
2281#endif /* defined(MBEDTLS_ECP_C) */
2282 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002283 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002284 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002285
2286exit:
2287 /* Fill the unused part of the output buffer (the whole buffer on error,
2288 * the trailing part on success) with something that isn't a valid mac
2289 * (barring an attack on the mac and deliberately-crafted input),
2290 * in case the caller doesn't check the return status properly. */
2291 if( status == PSA_SUCCESS )
2292 memset( signature + *signature_length, '!',
2293 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002294 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002295 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002296 /* If signature_size is 0 then we have nothing to do. We must not call
2297 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002298 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002299}
2300
2301psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
2302 psa_algorithm_t alg,
2303 const uint8_t *hash,
2304 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002305 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002306 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002307{
2308 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002309 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002310
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002311 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_VERIFY, alg );
2312 if( status != PSA_SUCCESS )
2313 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002314
Gilles Peskine61b91d42018-06-08 16:09:36 +02002315#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002316 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002317 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002318 return( psa_rsa_verify( slot->data.rsa,
2319 alg,
2320 hash, hash_length,
2321 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002322 }
2323 else
2324#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002325#if defined(MBEDTLS_ECP_C)
2326 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2327 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002328#if defined(MBEDTLS_ECDSA_C)
2329 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002330 return( psa_ecdsa_verify( slot->data.ecp,
2331 hash, hash_length,
2332 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002333 else
2334#endif /* defined(MBEDTLS_ECDSA_C) */
2335 {
2336 return( PSA_ERROR_INVALID_ARGUMENT );
2337 }
itayzafrir5c753392018-05-08 11:18:38 +03002338 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002339 else
2340#endif /* defined(MBEDTLS_ECP_C) */
2341 {
2342 return( PSA_ERROR_NOT_SUPPORTED );
2343 }
2344}
2345
Gilles Peskine072ac562018-06-30 00:21:29 +02002346#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2347static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2348 mbedtls_rsa_context *rsa )
2349{
2350 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2351 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2352 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2353 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2354}
2355#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2356
Gilles Peskine61b91d42018-06-08 16:09:36 +02002357psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
2358 psa_algorithm_t alg,
2359 const uint8_t *input,
2360 size_t input_length,
2361 const uint8_t *salt,
2362 size_t salt_length,
2363 uint8_t *output,
2364 size_t output_size,
2365 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002366{
2367 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002368 psa_status_t status;
2369
Darryl Green5cc689a2018-07-24 15:34:10 +01002370 (void) input;
2371 (void) input_length;
2372 (void) salt;
2373 (void) output;
2374 (void) output_size;
2375
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002376 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002377
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002378 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2379 return( PSA_ERROR_INVALID_ARGUMENT );
2380
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002381 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2382 if( status != PSA_SUCCESS )
2383 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002384 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2385 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002386 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002387
2388#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002389 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002390 {
2391 mbedtls_rsa_context *rsa = slot->data.rsa;
2392 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002393 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002394 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002395#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002396 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002397 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002398 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2399 mbedtls_ctr_drbg_random,
2400 &global_data.ctr_drbg,
2401 MBEDTLS_RSA_PUBLIC,
2402 input_length,
2403 input,
2404 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002405 }
2406 else
2407#endif /* MBEDTLS_PKCS1_V15 */
2408#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002409 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002410 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002411 psa_rsa_oaep_set_padding_mode( alg, rsa );
2412 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2413 mbedtls_ctr_drbg_random,
2414 &global_data.ctr_drbg,
2415 MBEDTLS_RSA_PUBLIC,
2416 salt, salt_length,
2417 input_length,
2418 input,
2419 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002420 }
2421 else
2422#endif /* MBEDTLS_PKCS1_V21 */
2423 {
2424 return( PSA_ERROR_INVALID_ARGUMENT );
2425 }
2426 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002427 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002428 return( mbedtls_to_psa_error( ret ) );
2429 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002430 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002431#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002432 {
2433 return( PSA_ERROR_NOT_SUPPORTED );
2434 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002435}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002436
Gilles Peskine61b91d42018-06-08 16:09:36 +02002437psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
2438 psa_algorithm_t alg,
2439 const uint8_t *input,
2440 size_t input_length,
2441 const uint8_t *salt,
2442 size_t salt_length,
2443 uint8_t *output,
2444 size_t output_size,
2445 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002446{
2447 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002448 psa_status_t status;
2449
Darryl Green5cc689a2018-07-24 15:34:10 +01002450 (void) input;
2451 (void) input_length;
2452 (void) salt;
2453 (void) output;
2454 (void) output_size;
2455
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002456 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002457
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002458 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2459 return( PSA_ERROR_INVALID_ARGUMENT );
2460
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002461 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2462 if( status != PSA_SUCCESS )
2463 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002464 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2465 return( PSA_ERROR_INVALID_ARGUMENT );
2466
2467#if defined(MBEDTLS_RSA_C)
2468 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2469 {
2470 mbedtls_rsa_context *rsa = slot->data.rsa;
2471 int ret;
2472
Gilles Peskine630a18a2018-06-29 17:49:35 +02002473 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002474 return( PSA_ERROR_INVALID_ARGUMENT );
2475
2476#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002477 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002478 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002479 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2480 mbedtls_ctr_drbg_random,
2481 &global_data.ctr_drbg,
2482 MBEDTLS_RSA_PRIVATE,
2483 output_length,
2484 input,
2485 output,
2486 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002487 }
2488 else
2489#endif /* MBEDTLS_PKCS1_V15 */
2490#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002491 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002492 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002493 psa_rsa_oaep_set_padding_mode( alg, rsa );
2494 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2495 mbedtls_ctr_drbg_random,
2496 &global_data.ctr_drbg,
2497 MBEDTLS_RSA_PRIVATE,
2498 salt, salt_length,
2499 output_length,
2500 input,
2501 output,
2502 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002503 }
2504 else
2505#endif /* MBEDTLS_PKCS1_V21 */
2506 {
2507 return( PSA_ERROR_INVALID_ARGUMENT );
2508 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002509
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002510 return( mbedtls_to_psa_error( ret ) );
2511 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002512 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002513#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002514 {
2515 return( PSA_ERROR_NOT_SUPPORTED );
2516 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002517}
Gilles Peskine20035e32018-02-03 22:44:14 +01002518
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002519
2520
mohammad1603503973b2018-03-12 15:59:30 +02002521/****************************************************************/
2522/* Symmetric cryptography */
2523/****************************************************************/
2524
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002525/* Initialize the cipher operation structure. Once this function has been
2526 * called, psa_cipher_abort can run and will do the right thing. */
2527static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2528 psa_algorithm_t alg )
2529{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002530 if( ! PSA_ALG_IS_CIPHER( alg ) )
2531 {
2532 memset( operation, 0, sizeof( *operation ) );
2533 return( PSA_ERROR_INVALID_ARGUMENT );
2534 }
2535
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002536 operation->alg = alg;
2537 operation->key_set = 0;
2538 operation->iv_set = 0;
2539 operation->iv_required = 1;
2540 operation->iv_size = 0;
2541 operation->block_size = 0;
2542 mbedtls_cipher_init( &operation->ctx.cipher );
2543 return( PSA_SUCCESS );
2544}
2545
Gilles Peskinee553c652018-06-04 16:22:46 +02002546static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
2547 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02002548 psa_algorithm_t alg,
2549 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002550{
2551 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2552 psa_status_t status;
2553 key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002554 size_t key_bits;
2555 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002556 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2557 PSA_KEY_USAGE_ENCRYPT :
2558 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002559
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002560 status = psa_cipher_init( operation, alg );
2561 if( status != PSA_SUCCESS )
2562 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002563
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002564 status = psa_get_key_from_slot( key, &slot, usage, alg);
2565 if( status != PSA_SUCCESS )
2566 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002567 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002568
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002569 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002570 if( cipher_info == NULL )
2571 return( PSA_ERROR_NOT_SUPPORTED );
2572
mohammad1603503973b2018-03-12 15:59:30 +02002573 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002574 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002575 {
2576 psa_cipher_abort( operation );
2577 return( mbedtls_to_psa_error( ret ) );
2578 }
2579
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002580#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002581 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002582 {
2583 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2584 unsigned char keys[24];
2585 memcpy( keys, slot->data.raw.data, 16 );
2586 memcpy( keys + 16, slot->data.raw.data, 8 );
2587 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2588 keys,
2589 192, cipher_operation );
2590 }
2591 else
2592#endif
2593 {
2594 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2595 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002596 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002597 }
Moran Peker41deec42018-04-04 15:43:05 +03002598 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002599 {
2600 psa_cipher_abort( operation );
2601 return( mbedtls_to_psa_error( ret ) );
2602 }
2603
mohammad16038481e742018-03-18 13:57:31 +02002604#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002605 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002606 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002607 case PSA_ALG_CBC_NO_PADDING:
2608 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2609 MBEDTLS_PADDING_NONE );
2610 break;
2611 case PSA_ALG_CBC_PKCS7:
2612 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2613 MBEDTLS_PADDING_PKCS7 );
2614 break;
2615 default:
2616 /* The algorithm doesn't involve padding. */
2617 ret = 0;
2618 break;
2619 }
2620 if( ret != 0 )
2621 {
2622 psa_cipher_abort( operation );
2623 return( mbedtls_to_psa_error( ret ) );
mohammad16038481e742018-03-18 13:57:31 +02002624 }
2625#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2626
mohammad1603503973b2018-03-12 15:59:30 +02002627 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002628 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2629 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
2630 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02002631 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002632 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002633 }
mohammad1603503973b2018-03-12 15:59:30 +02002634
Moran Peker395db872018-05-31 14:07:14 +03002635 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002636}
2637
Gilles Peskinefe119512018-07-08 21:39:34 +02002638psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
2639 psa_key_slot_t key,
2640 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002641{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002642 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002643}
2644
Gilles Peskinefe119512018-07-08 21:39:34 +02002645psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
2646 psa_key_slot_t key,
2647 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002648{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002649 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002650}
2651
Gilles Peskinefe119512018-07-08 21:39:34 +02002652psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2653 unsigned char *iv,
2654 size_t iv_size,
2655 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002656{
itayzafrir534bd7c2018-08-02 13:56:32 +03002657 psa_status_t status;
2658 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002659 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002660 {
2661 status = PSA_ERROR_BAD_STATE;
2662 goto exit;
2663 }
Moran Peker41deec42018-04-04 15:43:05 +03002664 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002665 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002666 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03002667 goto exit;
2668 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002669 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2670 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002671 if( ret != 0 )
2672 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002673 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002674 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002675 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002676
mohammad16038481e742018-03-18 13:57:31 +02002677 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03002678 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002679
Moran Peker395db872018-05-31 14:07:14 +03002680exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03002681 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03002682 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002683 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002684}
2685
Gilles Peskinefe119512018-07-08 21:39:34 +02002686psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2687 const unsigned char *iv,
2688 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002689{
itayzafrir534bd7c2018-08-02 13:56:32 +03002690 psa_status_t status;
2691 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002692 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002693 {
2694 status = PSA_ERROR_BAD_STATE;
2695 goto exit;
2696 }
Moran Pekera28258c2018-05-29 16:25:04 +03002697 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002698 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002699 status = PSA_ERROR_INVALID_ARGUMENT;
2700 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03002701 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002702 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
2703 status = mbedtls_to_psa_error( ret );
2704exit:
2705 if( status == PSA_SUCCESS )
2706 operation->iv_set = 1;
2707 else
mohammad1603503973b2018-03-12 15:59:30 +02002708 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002709 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002710}
2711
Gilles Peskinee553c652018-06-04 16:22:46 +02002712psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2713 const uint8_t *input,
2714 size_t input_length,
2715 unsigned char *output,
2716 size_t output_size,
2717 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002718{
itayzafrir534bd7c2018-08-02 13:56:32 +03002719 psa_status_t status;
2720 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002721 size_t expected_output_size;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002722 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002723 {
2724 /* Take the unprocessed partial block left over from previous
2725 * update calls, if any, plus the input to this call. Remove
2726 * the last partial block, if any. You get the data that will be
2727 * output in this call. */
2728 expected_output_size =
2729 ( operation->ctx.cipher.unprocessed_len + input_length )
2730 / operation->block_size * operation->block_size;
2731 }
2732 else
2733 {
2734 expected_output_size = input_length;
2735 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002736
Gilles Peskine89d789c2018-06-04 17:17:16 +02002737 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03002738 {
2739 status = PSA_ERROR_BUFFER_TOO_SMALL;
2740 goto exit;
2741 }
mohammad160382759612018-03-12 18:16:40 +02002742
mohammad1603503973b2018-03-12 15:59:30 +02002743 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002744 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03002745 status = mbedtls_to_psa_error( ret );
2746exit:
2747 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02002748 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002749 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002750}
2751
Gilles Peskinee553c652018-06-04 16:22:46 +02002752psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2753 uint8_t *output,
2754 size_t output_size,
2755 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002756{
Janos Follath315b51c2018-07-09 16:04:51 +01002757 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
2758 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002759 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002760
mohammad1603503973b2018-03-12 15:59:30 +02002761 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002762 {
Janos Follath315b51c2018-07-09 16:04:51 +01002763 status = PSA_ERROR_BAD_STATE;
2764 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002765 }
2766 if( operation->iv_required && ! operation->iv_set )
2767 {
Janos Follath315b51c2018-07-09 16:04:51 +01002768 status = PSA_ERROR_BAD_STATE;
2769 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002770 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002771
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002772 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002773 operation->alg == PSA_ALG_CBC_NO_PADDING &&
2774 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03002775 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002776 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01002777 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002778 }
2779
Janos Follath315b51c2018-07-09 16:04:51 +01002780 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
2781 temp_output_buffer,
2782 output_length );
2783 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002784 {
Janos Follath315b51c2018-07-09 16:04:51 +01002785 status = mbedtls_to_psa_error( cipher_ret );
2786 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02002787 }
Janos Follath315b51c2018-07-09 16:04:51 +01002788
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002789 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01002790 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002791 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002792 memcpy( output, temp_output_buffer, *output_length );
2793 else
2794 {
Janos Follath315b51c2018-07-09 16:04:51 +01002795 status = PSA_ERROR_BUFFER_TOO_SMALL;
2796 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002797 }
mohammad1603503973b2018-03-12 15:59:30 +02002798
Janos Follath279ab8e2018-07-09 16:13:21 +01002799 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002800 status = psa_cipher_abort( operation );
2801
2802 return( status );
2803
2804error:
2805
2806 *output_length = 0;
2807
Janos Follath279ab8e2018-07-09 16:13:21 +01002808 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002809 (void) psa_cipher_abort( operation );
2810
2811 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002812}
2813
Gilles Peskinee553c652018-06-04 16:22:46 +02002814psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2815{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002816 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02002817 {
2818 /* The object has (apparently) been initialized but it is not
2819 * in use. It's ok to call abort on such an object, and there's
2820 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002821 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02002822 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002823
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002824 /* Sanity check (shouldn't happen: operation->alg should
2825 * always have been initialized to a valid value). */
2826 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
2827 return( PSA_ERROR_BAD_STATE );
2828
mohammad1603503973b2018-03-12 15:59:30 +02002829 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002830
Moran Peker41deec42018-04-04 15:43:05 +03002831 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002832 operation->key_set = 0;
2833 operation->iv_set = 0;
2834 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002835 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002836 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002837
Moran Peker395db872018-05-31 14:07:14 +03002838 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002839}
2840
Gilles Peskinea0655c32018-04-30 17:06:50 +02002841
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002842
mohammad16038cc1cee2018-03-28 01:21:33 +03002843/****************************************************************/
2844/* Key Policy */
2845/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002846
mohammad160327010052018-07-03 13:16:15 +03002847#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02002848void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002849{
Gilles Peskine803ce742018-06-18 16:07:14 +02002850 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03002851}
2852
Gilles Peskine2d277862018-06-18 15:41:12 +02002853void psa_key_policy_set_usage( psa_key_policy_t *policy,
2854 psa_key_usage_t usage,
2855 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03002856{
mohammad16034eed7572018-03-28 05:14:59 -07002857 policy->usage = usage;
2858 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03002859}
2860
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002861psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002862{
mohammad16036df908f2018-04-02 08:34:15 -07002863 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03002864}
2865
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002866psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002867{
mohammad16036df908f2018-04-02 08:34:15 -07002868 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03002869}
mohammad160327010052018-07-03 13:16:15 +03002870#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002871
Gilles Peskine2d277862018-06-18 15:41:12 +02002872psa_status_t psa_set_key_policy( psa_key_slot_t key,
2873 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002874{
2875 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002876 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002877
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002878 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002879 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002880
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002881 status = psa_get_empty_key_slot( key, &slot );
2882 if( status != PSA_SUCCESS )
2883 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03002884
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002885 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
2886 PSA_KEY_USAGE_ENCRYPT |
2887 PSA_KEY_USAGE_DECRYPT |
2888 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02002889 PSA_KEY_USAGE_VERIFY |
2890 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07002891 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03002892
mohammad16036df908f2018-04-02 08:34:15 -07002893 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002894
2895 return( PSA_SUCCESS );
2896}
2897
Gilles Peskine2d277862018-06-18 15:41:12 +02002898psa_status_t psa_get_key_policy( psa_key_slot_t key,
2899 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002900{
2901 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002902 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002903
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002904 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002905 return( PSA_ERROR_INVALID_ARGUMENT );
2906
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002907 status = psa_get_key_slot( key, &slot );
2908 if( status != PSA_SUCCESS )
2909 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002910
mohammad16036df908f2018-04-02 08:34:15 -07002911 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002912
2913 return( PSA_SUCCESS );
2914}
Gilles Peskine20035e32018-02-03 22:44:14 +01002915
Gilles Peskinea0655c32018-04-30 17:06:50 +02002916
2917
mohammad1603804cd712018-03-20 22:44:08 +02002918/****************************************************************/
2919/* Key Lifetime */
2920/****************************************************************/
2921
Gilles Peskine2d277862018-06-18 15:41:12 +02002922psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
2923 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002924{
2925 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002926 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002927
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002928 status = psa_get_key_slot( key, &slot );
2929 if( status != PSA_SUCCESS )
2930 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002931
mohammad1603804cd712018-03-20 22:44:08 +02002932 *lifetime = slot->lifetime;
2933
2934 return( PSA_SUCCESS );
2935}
2936
Gilles Peskine2d277862018-06-18 15:41:12 +02002937psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
Jaeden Amero65fb2362018-06-26 13:55:30 +01002938 psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002939{
2940 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002941 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002942
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002943 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
2944 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
mohammad1603ba178512018-03-21 04:35:20 -07002945 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE)
2946 return( PSA_ERROR_INVALID_ARGUMENT );
2947
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002948 status = psa_get_empty_key_slot( key, &slot );
2949 if( status != PSA_SUCCESS )
2950 return( status );
mohammad1603804cd712018-03-20 22:44:08 +02002951
Moran Pekerd7326592018-05-29 16:56:39 +03002952 if( lifetime != PSA_KEY_LIFETIME_VOLATILE )
mohammad1603ba178512018-03-21 04:35:20 -07002953 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002954
mohammad1603060ad8a2018-03-20 14:28:38 -07002955 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02002956
2957 return( PSA_SUCCESS );
2958}
2959
Gilles Peskine20035e32018-02-03 22:44:14 +01002960
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002961
mohammad16035955c982018-04-26 00:53:03 +03002962/****************************************************************/
2963/* AEAD */
2964/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002965
Gilles Peskineedf9a652018-08-17 18:11:56 +02002966typedef struct
2967{
2968 key_slot_t *slot;
2969 const mbedtls_cipher_info_t *cipher_info;
2970 union
2971 {
2972#if defined(MBEDTLS_CCM_C)
2973 mbedtls_ccm_context ccm;
2974#endif /* MBEDTLS_CCM_C */
2975#if defined(MBEDTLS_GCM_C)
2976 mbedtls_gcm_context gcm;
2977#endif /* MBEDTLS_GCM_C */
2978 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002979 psa_algorithm_t core_alg;
2980 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02002981 uint8_t tag_length;
2982} aead_operation_t;
2983
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02002984static void psa_aead_abort( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02002985{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02002986 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02002987 {
2988#if defined(MBEDTLS_CCM_C)
2989 case PSA_ALG_CCM:
2990 mbedtls_ccm_free( &operation->ctx.ccm );
2991 break;
2992#endif /* MBEDTLS_CCM_C */
2993#if defined(MBEDTLS_CCM_C)
2994 case PSA_ALG_GCM:
2995 mbedtls_gcm_free( &operation->ctx.gcm );
2996 break;
2997#endif /* MBEDTLS_GCM_C */
2998 }
2999}
3000
3001static psa_status_t psa_aead_setup( aead_operation_t *operation,
3002 psa_key_slot_t key,
3003 psa_key_usage_t usage,
3004 psa_algorithm_t alg )
3005{
3006 psa_status_t status;
3007 size_t key_bits;
3008 mbedtls_cipher_id_t cipher_id;
3009
3010 status = psa_get_key_from_slot( key, &operation->slot, usage, alg );
3011 if( status != PSA_SUCCESS )
3012 return( status );
3013
3014 key_bits = psa_get_key_bits( operation->slot );
3015
3016 operation->cipher_info =
3017 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3018 &cipher_id );
3019 if( operation->cipher_info == NULL )
3020 return( PSA_ERROR_NOT_SUPPORTED );
3021
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003022 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003023 {
3024#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003025 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3026 operation->core_alg = PSA_ALG_CCM;
3027 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003028 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3029 return( PSA_ERROR_INVALID_ARGUMENT );
3030 mbedtls_ccm_init( &operation->ctx.ccm );
3031 status = mbedtls_to_psa_error(
3032 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3033 operation->slot->data.raw.data,
3034 (unsigned int) key_bits ) );
3035 if( status != 0 )
3036 goto cleanup;
3037 break;
3038#endif /* MBEDTLS_CCM_C */
3039
3040#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003041 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3042 operation->core_alg = PSA_ALG_GCM;
3043 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003044 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3045 return( PSA_ERROR_INVALID_ARGUMENT );
3046 mbedtls_gcm_init( &operation->ctx.gcm );
3047 status = mbedtls_to_psa_error(
3048 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3049 operation->slot->data.raw.data,
3050 (unsigned int) key_bits ) );
3051 break;
3052#endif /* MBEDTLS_GCM_C */
3053
3054 default:
3055 return( PSA_ERROR_NOT_SUPPORTED );
3056 }
3057
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003058 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3059 {
3060 status = PSA_ERROR_INVALID_ARGUMENT;
3061 goto cleanup;
3062 }
3063 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3064 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3065 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3066 * In both cases, mbedtls_xxx will validate the tag length below. */
3067
Gilles Peskineedf9a652018-08-17 18:11:56 +02003068 return( PSA_SUCCESS );
3069
3070cleanup:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003071 psa_aead_abort( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003072 return( status );
3073}
3074
mohammad16035955c982018-04-26 00:53:03 +03003075psa_status_t psa_aead_encrypt( psa_key_slot_t key,
3076 psa_algorithm_t alg,
3077 const uint8_t *nonce,
3078 size_t nonce_length,
3079 const uint8_t *additional_data,
3080 size_t additional_data_length,
3081 const uint8_t *plaintext,
3082 size_t plaintext_length,
3083 uint8_t *ciphertext,
3084 size_t ciphertext_size,
3085 size_t *ciphertext_length )
3086{
mohammad16035955c982018-04-26 00:53:03 +03003087 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003088 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003089 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003090
mohammad1603f08a5502018-06-03 15:05:47 +03003091 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003092
Gilles Peskineedf9a652018-08-17 18:11:56 +02003093 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003094 if( status != PSA_SUCCESS )
3095 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003096
Gilles Peskineedf9a652018-08-17 18:11:56 +02003097 /* For all currently supported modes, the tag is at the end of the
3098 * ciphertext. */
3099 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3100 {
3101 status = PSA_ERROR_BUFFER_TOO_SMALL;
3102 goto exit;
3103 }
3104 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003105
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003106 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003107 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003108 status = mbedtls_to_psa_error(
3109 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3110 MBEDTLS_GCM_ENCRYPT,
3111 plaintext_length,
3112 nonce, nonce_length,
3113 additional_data, additional_data_length,
3114 plaintext, ciphertext,
3115 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003116 }
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003117 else if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003118 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003119 status = mbedtls_to_psa_error(
3120 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3121 plaintext_length,
3122 nonce, nonce_length,
3123 additional_data,
3124 additional_data_length,
3125 plaintext, ciphertext,
3126 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003127 }
mohammad16035c8845f2018-05-09 05:40:09 -07003128 else
3129 {
mohammad1603554faad2018-06-03 15:07:38 +03003130 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003131 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003132
Gilles Peskineedf9a652018-08-17 18:11:56 +02003133 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3134 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003135
Gilles Peskineedf9a652018-08-17 18:11:56 +02003136exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003137 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003138 if( status == PSA_SUCCESS )
3139 *ciphertext_length = plaintext_length + operation.tag_length;
3140 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003141}
3142
Gilles Peskineee652a32018-06-01 19:23:52 +02003143/* Locate the tag in a ciphertext buffer containing the encrypted data
3144 * followed by the tag. Return the length of the part preceding the tag in
3145 * *plaintext_length. This is the size of the plaintext in modes where
3146 * the encrypted data has the same size as the plaintext, such as
3147 * CCM and GCM. */
3148static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3149 const uint8_t *ciphertext,
3150 size_t ciphertext_length,
3151 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003152 const uint8_t **p_tag )
3153{
3154 size_t payload_length;
3155 if( tag_length > ciphertext_length )
3156 return( PSA_ERROR_INVALID_ARGUMENT );
3157 payload_length = ciphertext_length - tag_length;
3158 if( payload_length > plaintext_size )
3159 return( PSA_ERROR_BUFFER_TOO_SMALL );
3160 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003161 return( PSA_SUCCESS );
3162}
3163
mohammad16035955c982018-04-26 00:53:03 +03003164psa_status_t psa_aead_decrypt( psa_key_slot_t key,
3165 psa_algorithm_t alg,
3166 const uint8_t *nonce,
3167 size_t nonce_length,
3168 const uint8_t *additional_data,
3169 size_t additional_data_length,
3170 const uint8_t *ciphertext,
3171 size_t ciphertext_length,
3172 uint8_t *plaintext,
3173 size_t plaintext_size,
3174 size_t *plaintext_length )
3175{
mohammad16035955c982018-04-26 00:53:03 +03003176 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003177 aead_operation_t operation;
3178 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003179
Gilles Peskineee652a32018-06-01 19:23:52 +02003180 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003181
Gilles Peskineedf9a652018-08-17 18:11:56 +02003182 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003183 if( status != PSA_SUCCESS )
3184 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003185
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003186 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003187 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003188 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003189 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003190 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003191 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003192 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003193
Gilles Peskineedf9a652018-08-17 18:11:56 +02003194 status = mbedtls_to_psa_error(
3195 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3196 ciphertext_length - operation.tag_length,
3197 nonce, nonce_length,
3198 additional_data,
3199 additional_data_length,
3200 tag, operation.tag_length,
3201 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003202 }
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003203 else if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003204 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003205 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003206 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003207 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003208 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003209 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003210
Gilles Peskineedf9a652018-08-17 18:11:56 +02003211 status = mbedtls_to_psa_error(
3212 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3213 ciphertext_length - operation.tag_length,
3214 nonce, nonce_length,
3215 additional_data,
3216 additional_data_length,
3217 ciphertext, plaintext,
3218 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003219 }
mohammad160339574652018-06-01 04:39:53 -07003220 else
3221 {
mohammad1603554faad2018-06-03 15:07:38 +03003222 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003223 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003224
Gilles Peskineedf9a652018-08-17 18:11:56 +02003225 if( status != PSA_SUCCESS && plaintext_size != 0 )
3226 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003227
Gilles Peskineedf9a652018-08-17 18:11:56 +02003228exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003229 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003230 if( status == PSA_SUCCESS )
3231 *plaintext_length = ciphertext_length - operation.tag_length;
3232 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003233}
3234
Gilles Peskinea0655c32018-04-30 17:06:50 +02003235
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003236
Gilles Peskine20035e32018-02-03 22:44:14 +01003237/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003238/* Generators */
3239/****************************************************************/
3240
3241psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3242{
3243 psa_status_t status = PSA_SUCCESS;
3244 if( generator->alg == 0 )
3245 {
3246 /* The object has (apparently) been initialized but it is not
3247 * in use. It's ok to call abort on such an object, and there's
3248 * nothing to do. */
3249 }
3250 else
Gilles Peskine751d9652018-09-18 12:05:44 +02003251 if( generator->alg == PSA_ALG_SELECT_RAW )
3252 {
3253 if( generator->ctx.buffer.data != NULL )
3254 {
3255 mbedtls_zeroize( generator->ctx.buffer.data,
3256 generator->ctx.buffer.size );
3257 mbedtls_free( generator->ctx.buffer.data );
3258 }
3259 }
3260 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003261#if defined(MBEDTLS_MD_C)
3262 if( PSA_ALG_IS_HKDF( generator->alg ) )
3263 {
3264 mbedtls_free( generator->ctx.hkdf.info );
3265 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3266 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003267 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) )
3268 {
3269 if( generator->ctx.tls12_prf.key != NULL )
3270 {
3271 mbedtls_zeroize( generator->ctx.tls12_prf.key,
3272 generator->ctx.tls12_prf.key_len );
3273 mbedtls_free( generator->ctx.tls12_prf.key );
3274 }
Hanno Becker580fba12018-11-13 20:50:45 +00003275
3276 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
3277 {
3278 mbedtls_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
3279 generator->ctx.tls12_prf.Ai_with_seed_len );
3280 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
3281 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003282 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003283 else
3284#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003285 {
3286 status = PSA_ERROR_BAD_STATE;
3287 }
3288 memset( generator, 0, sizeof( *generator ) );
3289 return( status );
3290}
3291
3292
3293psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3294 size_t *capacity)
3295{
3296 *capacity = generator->capacity;
3297 return( PSA_SUCCESS );
3298}
3299
Gilles Peskinebef7f142018-07-12 17:22:21 +02003300#if defined(MBEDTLS_MD_C)
3301/* Read some bytes from an HKDF-based generator. This performs a chunk
3302 * of the expand phase of the HKDF algorithm. */
3303static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3304 psa_algorithm_t hash_alg,
3305 uint8_t *output,
3306 size_t output_length )
3307{
3308 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3309 psa_status_t status;
3310
3311 while( output_length != 0 )
3312 {
3313 /* Copy what remains of the current block */
3314 uint8_t n = hash_length - hkdf->offset_in_block;
3315 if( n > output_length )
3316 n = (uint8_t) output_length;
3317 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3318 output += n;
3319 output_length -= n;
3320 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003321 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003322 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003323 /* We can't be wanting more output after block 0xff, otherwise
3324 * the capacity check in psa_generator_read() would have
3325 * prevented this call. It could happen only if the generator
3326 * object was corrupted or if this function is called directly
3327 * inside the library. */
3328 if( hkdf->block_number == 0xff )
3329 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003330
3331 /* We need a new block */
3332 ++hkdf->block_number;
3333 hkdf->offset_in_block = 0;
3334 status = psa_hmac_setup_internal( &hkdf->hmac,
3335 hkdf->prk, hash_length,
3336 hash_alg );
3337 if( status != PSA_SUCCESS )
3338 return( status );
3339 if( hkdf->block_number != 1 )
3340 {
3341 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3342 hkdf->output_block,
3343 hash_length );
3344 if( status != PSA_SUCCESS )
3345 return( status );
3346 }
3347 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3348 hkdf->info,
3349 hkdf->info_length );
3350 if( status != PSA_SUCCESS )
3351 return( status );
3352 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3353 &hkdf->block_number, 1 );
3354 if( status != PSA_SUCCESS )
3355 return( status );
3356 status = psa_hmac_finish_internal( &hkdf->hmac,
3357 hkdf->output_block,
3358 sizeof( hkdf->output_block ) );
3359 if( status != PSA_SUCCESS )
3360 return( status );
3361 }
3362
3363 return( PSA_SUCCESS );
3364}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003365
3366static psa_status_t psa_generator_tls12_prf_generate_next_block(
3367 psa_tls12_prf_generator_t *tls12_prf,
3368 psa_algorithm_t alg )
3369{
3370 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3371 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3372 psa_hmac_internal_data hmac;
3373 psa_status_t status, cleanup_status;
3374
3375 /* We can't be wanting more output after block 0xff, otherwise
3376 * the capacity check in psa_generator_read() would have
3377 * prevented this call. It could happen only if the generator
3378 * object was corrupted or if this function is called directly
3379 * inside the library. */
3380 if( tls12_prf->block_number == 0xff )
3381 return( PSA_ERROR_BAD_STATE );
3382
3383 /* We need a new block */
3384 ++tls12_prf->block_number;
3385 tls12_prf->offset_in_block = 0;
3386
3387 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
3388 *
3389 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
3390 *
3391 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
3392 * HMAC_hash(secret, A(2) + seed) +
3393 * HMAC_hash(secret, A(3) + seed) + ...
3394 *
3395 * A(0) = seed
3396 * A(i) = HMAC_hash( secret, A(i-1) )
3397 *
3398 * The `psa_tls12_prf_generator` structures saves the block
3399 * `HMAC_hash(secret, A(i) + seed)` from which the output
3400 * is currently extracted as `output_block`, while
3401 * `A(i) + seed` is stored in `Ai_with_seed`.
3402 *
3403 * Generating a new block means recalculating `Ai_with_seed`
3404 * from the A(i)-part of it, and afterwards recalculating
3405 * `output_block`.
3406 *
3407 * A(0) is computed at setup time.
3408 *
3409 */
3410
3411 psa_hmac_init_internal( &hmac );
3412
3413 /* We must distinguish the calculation of A(1) from those
3414 * of A(2) and higher, because A(0)=seed has a different
3415 * length than the other A(i). */
3416 if( tls12_prf->block_number == 1 )
3417 {
3418 /* Compute A(1) = HMAC_hash(secret, label + seed) */
3419 status = psa_hmac_setup_internal( &hmac,
3420 tls12_prf->key,
3421 tls12_prf->key_len,
3422 hash_alg );
3423 if( status != PSA_SUCCESS )
3424 goto cleanup;
3425
3426 status = psa_hash_update( &hmac.hash_ctx,
3427 /* This omits the (so far undefined)
3428 * first hash_length bytes. */
3429 tls12_prf->Ai_with_seed + hash_length,
Hanno Becker580fba12018-11-13 20:50:45 +00003430 tls12_prf->Ai_with_seed_len - hash_length );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003431 if( status != PSA_SUCCESS )
3432 goto cleanup;
3433 status = psa_hmac_finish_internal( &hmac,
3434 tls12_prf->Ai_with_seed,
3435 hash_length );
3436 if( status != PSA_SUCCESS )
3437 goto cleanup;
3438 }
3439 else
3440 {
3441 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
3442 status = psa_hmac_setup_internal( &hmac,
3443 tls12_prf->key,
3444 tls12_prf->key_len,
3445 hash_alg );
3446 if( status != PSA_SUCCESS )
3447 goto cleanup;
3448
3449 status = psa_hash_update( &hmac.hash_ctx,
3450 tls12_prf->Ai_with_seed,
3451 /* This omits the seed part of A(i) */
3452 hash_length );
3453 if( status != PSA_SUCCESS )
3454 goto cleanup;
3455
3456 status = psa_hmac_finish_internal( &hmac,
3457 tls12_prf->Ai_with_seed,
3458 hash_length );
3459 if( status != PSA_SUCCESS )
3460 goto cleanup;
3461 }
3462
3463 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
3464 status = psa_hmac_setup_internal( &hmac,
3465 tls12_prf->key,
3466 tls12_prf->key_len,
3467 hash_alg );
3468 if( status != PSA_SUCCESS )
3469 goto cleanup;
3470
3471 status = psa_hash_update( &hmac.hash_ctx,
3472 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003473 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003474 if( status != PSA_SUCCESS )
3475 goto cleanup;
3476
3477 status = psa_hmac_finish_internal( &hmac,
3478 tls12_prf->output_block,
3479 hash_length );
3480 if( status != PSA_SUCCESS )
3481 goto cleanup;
3482
3483cleanup:
3484
3485 cleanup_status = psa_hmac_abort_internal( &hmac );
3486 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
3487 status = cleanup_status;
3488
3489 return( status );
3490}
3491
3492/* Read some bytes from an TLS-1.2-PRF-based generator.
3493 * See Section 5 of RFC 5246. */
3494static psa_status_t psa_generator_tls12_prf_read(
3495 psa_tls12_prf_generator_t *tls12_prf,
3496 psa_algorithm_t alg,
3497 uint8_t *output,
3498 size_t output_length )
3499{
3500 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3501 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3502 psa_status_t status;
3503
3504 while( output_length != 0 )
3505 {
3506 /* Copy what remains of the current block */
3507 uint8_t n = hash_length - tls12_prf->offset_in_block;
3508
3509 /* Check if we have fully processed the current block. */
3510 if( n == 0 )
3511 {
3512 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
3513 alg );
3514 if( status != PSA_SUCCESS )
3515 return( status );
3516
3517 continue;
3518 }
3519
3520 if( n > output_length )
3521 n = (uint8_t) output_length;
3522 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
3523 n );
3524 output += n;
3525 output_length -= n;
3526 tls12_prf->offset_in_block += n;
3527 }
3528
3529 return( PSA_SUCCESS );
3530}
Gilles Peskinebef7f142018-07-12 17:22:21 +02003531#endif /* MBEDTLS_MD_C */
3532
Gilles Peskineeab56e42018-07-12 17:12:33 +02003533psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3534 uint8_t *output,
3535 size_t output_length )
3536{
3537 psa_status_t status;
3538
3539 if( output_length > generator->capacity )
3540 {
3541 generator->capacity = 0;
3542 /* Go through the error path to wipe all confidential data now
3543 * that the generator object is useless. */
3544 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3545 goto exit;
3546 }
3547 if( output_length == 0 &&
3548 generator->capacity == 0 && generator->alg == 0 )
3549 {
3550 /* Edge case: this is a blank or finished generator, and 0
3551 * bytes were requested. The right error in this case could
3552 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3553 * INSUFFICIENT_CAPACITY, which is right for a finished
3554 * generator, for consistency with the case when
3555 * output_length > 0. */
3556 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3557 }
3558 generator->capacity -= output_length;
3559
Gilles Peskine751d9652018-09-18 12:05:44 +02003560 if( generator->alg == PSA_ALG_SELECT_RAW )
3561 {
Gilles Peskine211a4362018-10-25 22:22:31 +02003562 /* Initially, the capacity of a selection generator is always
3563 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
3564 * abbreviated in this comment as `size`. When the remaining
3565 * capacity is `c`, the next bytes to serve start `c` bytes
3566 * from the end of the buffer, i.e. `size - c` from the
3567 * beginning of the buffer. Since `generator->capacity` was just
3568 * decremented above, we need to serve the bytes from
3569 * `size - generator->capacity - output_length` to
3570 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02003571 size_t offset =
3572 generator->ctx.buffer.size - generator->capacity - output_length;
3573 memcpy( output, generator->ctx.buffer.data + offset, output_length );
3574 status = PSA_SUCCESS;
3575 }
3576 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003577#if defined(MBEDTLS_MD_C)
3578 if( PSA_ALG_IS_HKDF( generator->alg ) )
3579 {
3580 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3581 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3582 output, output_length );
3583 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003584 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) )
3585 {
3586 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
3587 generator->alg, output,
3588 output_length );
3589 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003590 else
3591#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003592 {
3593 return( PSA_ERROR_BAD_STATE );
3594 }
3595
3596exit:
3597 if( status != PSA_SUCCESS )
3598 {
3599 psa_generator_abort( generator );
3600 memset( output, '!', output_length );
3601 }
3602 return( status );
3603}
3604
Gilles Peskine08542d82018-07-19 17:05:42 +02003605#if defined(MBEDTLS_DES_C)
3606static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3607{
3608 if( data_size >= 8 )
3609 mbedtls_des_key_set_parity( data );
3610 if( data_size >= 16 )
3611 mbedtls_des_key_set_parity( data + 8 );
3612 if( data_size >= 24 )
3613 mbedtls_des_key_set_parity( data + 16 );
3614}
3615#endif /* MBEDTLS_DES_C */
3616
Gilles Peskineeab56e42018-07-12 17:12:33 +02003617psa_status_t psa_generator_import_key( psa_key_slot_t key,
3618 psa_key_type_t type,
3619 size_t bits,
3620 psa_crypto_generator_t *generator )
3621{
3622 uint8_t *data = NULL;
3623 size_t bytes = PSA_BITS_TO_BYTES( bits );
3624 psa_status_t status;
3625
3626 if( ! key_type_is_raw_bytes( type ) )
3627 return( PSA_ERROR_INVALID_ARGUMENT );
3628 if( bits % 8 != 0 )
3629 return( PSA_ERROR_INVALID_ARGUMENT );
3630 data = mbedtls_calloc( 1, bytes );
3631 if( data == NULL )
3632 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3633
3634 status = psa_generator_read( generator, data, bytes );
3635 if( status != PSA_SUCCESS )
3636 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003637#if defined(MBEDTLS_DES_C)
3638 if( type == PSA_KEY_TYPE_DES )
3639 psa_des_set_key_parity( data, bytes );
3640#endif /* MBEDTLS_DES_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003641 status = psa_import_key( key, type, data, bytes );
3642
3643exit:
3644 mbedtls_free( data );
3645 return( status );
3646}
3647
3648
3649
3650/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003651/* Key derivation */
3652/****************************************************************/
3653
Gilles Peskinebef7f142018-07-12 17:22:21 +02003654/* Set up an HKDF-based generator. This is exactly the extract phase
3655 * of the HKDF algorithm. */
3656static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003657 const uint8_t *secret,
3658 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02003659 psa_algorithm_t hash_alg,
3660 const uint8_t *salt,
3661 size_t salt_length,
3662 const uint8_t *label,
3663 size_t label_length )
3664{
3665 psa_status_t status;
3666 status = psa_hmac_setup_internal( &hkdf->hmac,
3667 salt, salt_length,
Gilles Peskine00709fa2018-08-22 18:25:41 +02003668 PSA_ALG_HMAC_GET_HASH( hash_alg ) );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003669 if( status != PSA_SUCCESS )
3670 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003671 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003672 if( status != PSA_SUCCESS )
3673 return( status );
3674 status = psa_hmac_finish_internal( &hkdf->hmac,
3675 hkdf->prk,
3676 sizeof( hkdf->prk ) );
3677 if( status != PSA_SUCCESS )
3678 return( status );
3679 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
3680 hkdf->block_number = 0;
3681 hkdf->info_length = label_length;
3682 if( label_length != 0 )
3683 {
3684 hkdf->info = mbedtls_calloc( 1, label_length );
3685 if( hkdf->info == NULL )
3686 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3687 memcpy( hkdf->info, label, label_length );
3688 }
3689 return( PSA_SUCCESS );
3690}
3691
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003692/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5). */
3693static psa_status_t psa_generator_tls12_prf_setup(
3694 psa_tls12_prf_generator_t *tls12_prf,
3695 const unsigned char *key,
3696 size_t key_len,
3697 psa_algorithm_t hash_alg,
3698 const uint8_t *salt,
3699 size_t salt_length,
3700 const uint8_t *label,
3701 size_t label_length )
3702{
3703 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00003704 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
3705 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003706
3707 tls12_prf->key = mbedtls_calloc( 1, key_len );
3708 if( tls12_prf->key == NULL )
3709 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3710 tls12_prf->key_len = key_len;
3711 memcpy( tls12_prf->key, key, key_len );
3712
Hanno Becker580fba12018-11-13 20:50:45 +00003713 overflow = ( salt_length + label_length < salt_length ) ||
3714 ( salt_length + label_length + hash_length < hash_length );
3715 if( overflow )
3716 return( PSA_ERROR_INVALID_ARGUMENT );
3717
3718 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
3719 if( tls12_prf->Ai_with_seed == NULL )
3720 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3721 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
3722
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003723 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
3724 * leaving the initial `hash_length` bytes unspecified for now. */
3725 memcpy( tls12_prf->Ai_with_seed + hash_length, label, label_length );
3726 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
3727 salt, salt_length );
3728
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003729 /* The first block gets generated when
3730 * psa_generator_read() is called. */
3731 tls12_prf->block_number = 0;
3732 tls12_prf->offset_in_block = hash_length;
3733
3734 return( PSA_SUCCESS );
3735}
3736
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003737static psa_status_t psa_key_derivation_internal(
3738 psa_crypto_generator_t *generator,
3739 const uint8_t *secret, size_t secret_length,
3740 psa_algorithm_t alg,
3741 const uint8_t *salt, size_t salt_length,
3742 const uint8_t *label, size_t label_length,
3743 size_t capacity )
3744{
3745 psa_status_t status;
3746 size_t max_capacity;
3747
3748 /* Set generator->alg even on failure so that abort knows what to do. */
3749 generator->alg = alg;
3750
Gilles Peskine751d9652018-09-18 12:05:44 +02003751 if( alg == PSA_ALG_SELECT_RAW )
3752 {
3753 if( salt_length != 0 )
3754 return( PSA_ERROR_INVALID_ARGUMENT );
3755 if( label_length != 0 )
3756 return( PSA_ERROR_INVALID_ARGUMENT );
3757 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
3758 if( generator->ctx.buffer.data == NULL )
3759 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3760 memcpy( generator->ctx.buffer.data, secret, secret_length );
3761 generator->ctx.buffer.size = secret_length;
3762 max_capacity = secret_length;
3763 status = PSA_SUCCESS;
3764 }
3765 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003766#if defined(MBEDTLS_MD_C)
3767 if( PSA_ALG_IS_HKDF( alg ) )
3768 {
3769 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3770 size_t hash_size = PSA_HASH_SIZE( hash_alg );
3771 if( hash_size == 0 )
3772 return( PSA_ERROR_NOT_SUPPORTED );
3773 max_capacity = 255 * hash_size;
3774 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
3775 secret, secret_length,
3776 hash_alg,
3777 salt, salt_length,
3778 label, label_length );
3779 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003780 else if( PSA_ALG_IS_TLS12_PRF( alg ) )
3781 {
3782 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3783 size_t hash_size = PSA_HASH_SIZE( hash_alg );
3784
3785 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
3786 if( hash_alg != PSA_ALG_SHA_256 &&
3787 hash_alg != PSA_ALG_SHA_384 )
3788 {
3789 return( PSA_ERROR_NOT_SUPPORTED );
3790 }
3791
3792 max_capacity = 255 * hash_size;
3793 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
3794 secret, secret_length,
3795 hash_alg, salt, salt_length,
3796 label, label_length );
3797 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003798 else
3799#endif
3800 {
3801 return( PSA_ERROR_NOT_SUPPORTED );
3802 }
3803
3804 if( status != PSA_SUCCESS )
3805 return( status );
3806
3807 if( capacity <= max_capacity )
3808 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02003809 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
3810 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003811 else
3812 return( PSA_ERROR_INVALID_ARGUMENT );
3813
3814 return( PSA_SUCCESS );
3815}
3816
Gilles Peskineea0fb492018-07-12 17:17:20 +02003817psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Darryl Green88001362018-07-26 13:59:04 +01003818 psa_key_slot_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +02003819 psa_algorithm_t alg,
3820 const uint8_t *salt,
3821 size_t salt_length,
3822 const uint8_t *label,
3823 size_t label_length,
3824 size_t capacity )
3825{
3826 key_slot_t *slot;
3827 psa_status_t status;
3828
3829 if( generator->alg != 0 )
3830 return( PSA_ERROR_BAD_STATE );
3831
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003832 /* Make sure that alg is a key derivation algorithm. This prevents
3833 * key selection algorithms, which psa_key_derivation_internal
3834 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003835 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
3836 return( PSA_ERROR_INVALID_ARGUMENT );
3837
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003838 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DERIVE, alg );
3839 if( status != PSA_SUCCESS )
3840 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003841
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003842 if( slot->type != PSA_KEY_TYPE_DERIVE )
3843 return( PSA_ERROR_INVALID_ARGUMENT );
3844
3845 status = psa_key_derivation_internal( generator,
3846 slot->data.raw.data,
3847 slot->data.raw.bytes,
3848 alg,
3849 salt, salt_length,
3850 label, label_length,
3851 capacity );
3852 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02003853 psa_generator_abort( generator );
3854 return( status );
3855}
3856
3857
3858
3859/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02003860/* Key agreement */
3861/****************************************************************/
3862
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02003863static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
3864 size_t peer_key_length,
3865 const mbedtls_ecp_keypair *our_key,
3866 uint8_t *shared_secret,
3867 size_t shared_secret_size,
3868 size_t *shared_secret_length )
3869{
3870 mbedtls_pk_context pk;
3871 mbedtls_ecp_keypair *their_key = NULL;
3872 mbedtls_ecdh_context ecdh;
3873 int ret;
3874 mbedtls_ecdh_init( &ecdh );
3875 mbedtls_pk_init( &pk );
3876
3877 ret = mbedtls_pk_parse_public_key( &pk, peer_key, peer_key_length );
3878 if( ret != 0 )
3879 goto exit;
Gilles Peskine88714d72018-10-25 23:07:25 +02003880 switch( mbedtls_pk_get_type( &pk ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02003881 {
Gilles Peskine88714d72018-10-25 23:07:25 +02003882 case MBEDTLS_PK_ECKEY:
3883 case MBEDTLS_PK_ECKEY_DH:
3884 break;
3885 default:
3886 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
3887 goto exit;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02003888 }
3889 their_key = mbedtls_pk_ec( pk );
Gilles Peskineb4086612018-11-14 20:51:23 +01003890 if( their_key->grp.id != our_key->grp.id )
3891 {
3892 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
3893 goto exit;
3894 }
3895
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02003896 ret = mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS );
3897 if( ret != 0 )
3898 goto exit;
3899 ret = mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS );
3900 if( ret != 0 )
3901 goto exit;
3902
3903 ret = mbedtls_ecdh_calc_secret( &ecdh,
3904 shared_secret_length,
3905 shared_secret, shared_secret_size,
3906 mbedtls_ctr_drbg_random,
3907 &global_data.ctr_drbg );
3908
3909exit:
3910 mbedtls_pk_free( &pk );
3911 mbedtls_ecdh_free( &ecdh );
3912 return( mbedtls_to_psa_error( ret ) );
3913}
3914
Gilles Peskine01d718c2018-09-18 12:01:02 +02003915#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
3916
3917static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
3918 key_slot_t *private_key,
3919 const uint8_t *peer_key,
3920 size_t peer_key_length,
3921 psa_algorithm_t alg )
3922{
3923 psa_status_t status;
3924 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
3925 size_t shared_secret_length = 0;
3926
3927 /* Step 1: run the secret agreement algorithm to generate the shared
3928 * secret. */
3929 switch( PSA_ALG_KEY_AGREEMENT_GET_BASE( alg ) )
3930 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02003931#if defined(MBEDTLS_ECDH_C)
3932 case PSA_ALG_ECDH_BASE:
3933 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
3934 return( PSA_ERROR_INVALID_ARGUMENT );
3935 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
3936 private_key->data.ecp,
3937 shared_secret,
3938 sizeof( shared_secret ),
3939 &shared_secret_length );
3940 break;
3941#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02003942 default:
3943 return( PSA_ERROR_NOT_SUPPORTED );
3944 }
3945 if( status != PSA_SUCCESS )
3946 goto exit;
3947
3948 /* Step 2: set up the key derivation to generate key material from
3949 * the shared secret. */
3950 status = psa_key_derivation_internal( generator,
3951 shared_secret, shared_secret_length,
3952 PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ),
3953 NULL, 0, NULL, 0,
3954 PSA_GENERATOR_UNBRIDLED_CAPACITY );
3955exit:
3956 mbedtls_zeroize( shared_secret, shared_secret_length );
3957 return( status );
3958}
3959
3960psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
3961 psa_key_slot_t private_key,
3962 const uint8_t *peer_key,
3963 size_t peer_key_length,
3964 psa_algorithm_t alg )
3965{
3966 key_slot_t *slot;
3967 psa_status_t status;
3968 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
3969 return( PSA_ERROR_INVALID_ARGUMENT );
3970 status = psa_get_key_from_slot( private_key, &slot,
3971 PSA_KEY_USAGE_DERIVE, alg );
3972 if( status != PSA_SUCCESS )
3973 return( status );
3974 return( psa_key_agreement_internal( generator,
3975 slot,
3976 peer_key, peer_key_length,
3977 alg ) );
3978}
3979
3980
3981
3982/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003983/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02003984/****************************************************************/
3985
3986psa_status_t psa_generate_random( uint8_t *output,
3987 size_t output_size )
3988{
itayzafrir0adf0fc2018-09-06 16:24:41 +03003989 int ret;
3990 GUARD_MODULE_INITIALIZED;
3991
3992 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02003993 return( mbedtls_to_psa_error( ret ) );
3994}
3995
3996psa_status_t psa_generate_key( psa_key_slot_t key,
3997 psa_key_type_t type,
3998 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02003999 const void *extra,
4000 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02004001{
Gilles Peskine12313cd2018-06-20 00:20:32 +02004002 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004003 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004004
Gilles Peskine53d991e2018-07-12 01:14:59 +02004005 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004006 return( PSA_ERROR_INVALID_ARGUMENT );
4007
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004008 status = psa_get_empty_key_slot( key, &slot );
4009 if( status != PSA_SUCCESS )
4010 return( status );
4011
Gilles Peskine48c0ea12018-06-21 14:15:31 +02004012 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004013 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004014 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004015 if( status != PSA_SUCCESS )
4016 return( status );
4017 status = psa_generate_random( slot->data.raw.data,
4018 slot->data.raw.bytes );
4019 if( status != PSA_SUCCESS )
4020 {
4021 mbedtls_free( slot->data.raw.data );
4022 return( status );
4023 }
4024#if defined(MBEDTLS_DES_C)
4025 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004026 psa_des_set_key_parity( slot->data.raw.data,
4027 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004028#endif /* MBEDTLS_DES_C */
4029 }
4030 else
4031
4032#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
4033 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
4034 {
4035 mbedtls_rsa_context *rsa;
4036 int ret;
4037 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02004038 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
4039 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01004040 /* Accept only byte-aligned keys, for the same reasons as
4041 * in psa_import_rsa_key(). */
4042 if( bits % 8 != 0 )
4043 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02004044 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004045 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02004046 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004047 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004048 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02004049#if INT_MAX < 0xffffffff
4050 /* Check that the uint32_t value passed by the caller fits
4051 * in the range supported by this implementation. */
4052 if( p->e > INT_MAX )
4053 return( PSA_ERROR_NOT_SUPPORTED );
4054#endif
4055 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004056 }
4057 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
4058 if( rsa == NULL )
4059 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4060 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
4061 ret = mbedtls_rsa_gen_key( rsa,
4062 mbedtls_ctr_drbg_random,
4063 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004064 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02004065 exponent );
4066 if( ret != 0 )
4067 {
4068 mbedtls_rsa_free( rsa );
4069 mbedtls_free( rsa );
4070 return( mbedtls_to_psa_error( ret ) );
4071 }
4072 slot->data.rsa = rsa;
4073 }
4074 else
4075#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
4076
4077#if defined(MBEDTLS_ECP_C)
4078 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
4079 {
4080 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
4081 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
4082 const mbedtls_ecp_curve_info *curve_info =
4083 mbedtls_ecp_curve_info_from_grp_id( grp_id );
4084 mbedtls_ecp_keypair *ecp;
4085 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004086 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004087 return( PSA_ERROR_NOT_SUPPORTED );
4088 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
4089 return( PSA_ERROR_NOT_SUPPORTED );
4090 if( curve_info->bit_size != bits )
4091 return( PSA_ERROR_INVALID_ARGUMENT );
4092 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
4093 if( ecp == NULL )
4094 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4095 mbedtls_ecp_keypair_init( ecp );
4096 ret = mbedtls_ecp_gen_key( grp_id, ecp,
4097 mbedtls_ctr_drbg_random,
4098 &global_data.ctr_drbg );
4099 if( ret != 0 )
4100 {
4101 mbedtls_ecp_keypair_free( ecp );
4102 mbedtls_free( ecp );
4103 return( mbedtls_to_psa_error( ret ) );
4104 }
4105 slot->data.ecp = ecp;
4106 }
4107 else
4108#endif /* MBEDTLS_ECP_C */
4109
4110 return( PSA_ERROR_NOT_SUPPORTED );
4111
4112 slot->type = type;
4113 return( PSA_SUCCESS );
Gilles Peskine05d69892018-06-19 22:00:52 +02004114}
4115
4116
4117/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01004118/* Module setup */
4119/****************************************************************/
4120
Gilles Peskinee59236f2018-01-27 23:32:46 +01004121void mbedtls_psa_crypto_free( void )
4122{
Jaeden Amero045bd502018-06-26 14:00:08 +01004123 psa_key_slot_t key;
Gilles Peskine9a056342018-08-01 15:46:54 +02004124 for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01004125 psa_destroy_key( key );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004126 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
4127 mbedtls_entropy_free( &global_data.entropy );
4128 mbedtls_zeroize( &global_data, sizeof( global_data ) );
4129}
4130
4131psa_status_t psa_crypto_init( void )
4132{
4133 int ret;
4134 const unsigned char drbg_seed[] = "PSA";
4135
4136 if( global_data.initialized != 0 )
4137 return( PSA_SUCCESS );
4138
4139 mbedtls_zeroize( &global_data, sizeof( global_data ) );
4140 mbedtls_entropy_init( &global_data.entropy );
4141 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
4142
4143 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
4144 mbedtls_entropy_func,
4145 &global_data.entropy,
4146 drbg_seed, sizeof( drbg_seed ) - 1 );
4147 if( ret != 0 )
4148 goto exit;
4149
Gilles Peskinee4ebc122018-03-07 14:16:44 +01004150 global_data.initialized = 1;
4151
Gilles Peskinee59236f2018-01-27 23:32:46 +01004152exit:
4153 if( ret != 0 )
4154 mbedtls_psa_crypto_free( );
4155 return( mbedtls_to_psa_error( ret ) );
4156}
4157
4158#endif /* MBEDTLS_PSA_CRYPTO_C */