blob: ffc587a1ee2dbbbaf460d14bc5159c3f655e5011 [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)
29
30#include "psa/crypto.h"
31
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010032#include <stdlib.h>
33#include <string.h>
34#if defined(MBEDTLS_PLATFORM_C)
35#include "mbedtls/platform.h"
36#else
37#define mbedtls_calloc calloc
38#define mbedtls_free free
39#endif
40
Gilles Peskinea5905292018-02-07 20:59:33 +010041#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020042#include "mbedtls/asn1.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020043#include "mbedtls/asn1write.h"
44#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010045#include "mbedtls/blowfish.h"
46#include "mbedtls/camellia.h"
47#include "mbedtls/cipher.h"
48#include "mbedtls/ccm.h"
49#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010050#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010051#include "mbedtls/des.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010052#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010053#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010054#include "mbedtls/error.h"
55#include "mbedtls/gcm.h"
56#include "mbedtls/md2.h"
57#include "mbedtls/md4.h"
58#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010059#include "mbedtls/md.h"
60#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010061#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010062#include "mbedtls/pk_internal.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010063#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010064#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010065#include "mbedtls/sha1.h"
66#include "mbedtls/sha256.h"
67#include "mbedtls/sha512.h"
68#include "mbedtls/xtea.h"
69
Gilles Peskinee59236f2018-01-27 23:32:46 +010070
71
72/* Implementation that should never be optimized out by the compiler */
73static void mbedtls_zeroize( void *v, size_t n )
74{
75 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
76}
77
Gilles Peskine9ef733f2018-02-07 21:05:37 +010078/* constant-time buffer comparison */
79static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
80{
81 size_t i;
82 unsigned char diff = 0;
83
84 for( i = 0; i < n; i++ )
85 diff |= a[i] ^ b[i];
86
87 return( diff );
88}
89
90
91
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010092/****************************************************************/
93/* Global data, support functions and library management */
94/****************************************************************/
95
96/* Number of key slots (plus one because 0 is not used).
97 * The value is a compile-time constant for now, for simplicity. */
Gilles Peskine828ed142018-06-18 23:25:51 +020098#define PSA_KEY_SLOT_COUNT 32
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010099
Gilles Peskine2d277862018-06-18 15:41:12 +0200100typedef struct
101{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100102 psa_key_type_t type;
mohammad16038cc1cee2018-03-28 01:21:33 +0300103 psa_key_policy_t policy;
mohammad1603804cd712018-03-20 22:44:08 +0200104 psa_key_lifetime_t lifetime;
Gilles Peskine2d277862018-06-18 15:41:12 +0200105 union
106 {
107 struct raw_data
108 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100109 uint8_t *data;
110 size_t bytes;
111 } raw;
Gilles Peskine969ac722018-01-28 18:16:59 +0100112#if defined(MBEDTLS_RSA_C)
113 mbedtls_rsa_context *rsa;
114#endif /* MBEDTLS_RSA_C */
115#if defined(MBEDTLS_ECP_C)
116 mbedtls_ecp_keypair *ecp;
117#endif /* MBEDTLS_ECP_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100118 } data;
119} key_slot_t;
120
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200121static int key_type_is_raw_bytes( psa_key_type_t type )
122{
123 psa_key_type_t category = type & PSA_KEY_TYPE_CATEGORY_MASK;
124 return( category == PSA_KEY_TYPE_RAW_DATA ||
125 category == PSA_KEY_TYPE_CATEGORY_SYMMETRIC );
126}
127
Gilles Peskine2d277862018-06-18 15:41:12 +0200128typedef struct
129{
Gilles Peskinee59236f2018-01-27 23:32:46 +0100130 int initialized;
131 mbedtls_entropy_context entropy;
132 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskine828ed142018-06-18 23:25:51 +0200133 key_slot_t key_slots[PSA_KEY_SLOT_COUNT];
Gilles Peskinee59236f2018-01-27 23:32:46 +0100134} psa_global_data_t;
135
136static psa_global_data_t global_data;
137
138static psa_status_t mbedtls_to_psa_error( int ret )
139{
Gilles Peskinea5905292018-02-07 20:59:33 +0100140 /* If there's both a high-level code and low-level code, dispatch on
141 * the high-level code. */
142 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100143 {
144 case 0:
145 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100146
147 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
148 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
149 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
150 return( PSA_ERROR_NOT_SUPPORTED );
151 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
152 return( PSA_ERROR_HARDWARE_FAILURE );
153
154 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
155 return( PSA_ERROR_HARDWARE_FAILURE );
156
Gilles Peskine9a944802018-06-21 09:35:35 +0200157 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
158 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
159 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
160 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
161 case MBEDTLS_ERR_ASN1_INVALID_DATA:
162 return( PSA_ERROR_INVALID_ARGUMENT );
163 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
164 return( PSA_ERROR_INSUFFICIENT_MEMORY );
165 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
166 return( PSA_ERROR_BUFFER_TOO_SMALL );
167
Gilles Peskinea5905292018-02-07 20:59:33 +0100168 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
169 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
170 return( PSA_ERROR_NOT_SUPPORTED );
171 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
172 return( PSA_ERROR_HARDWARE_FAILURE );
173
174 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
175 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
176 return( PSA_ERROR_NOT_SUPPORTED );
177 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
178 return( PSA_ERROR_HARDWARE_FAILURE );
179
180 case MBEDTLS_ERR_CCM_BAD_INPUT:
181 return( PSA_ERROR_INVALID_ARGUMENT );
182 case MBEDTLS_ERR_CCM_AUTH_FAILED:
183 return( PSA_ERROR_INVALID_SIGNATURE );
184 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
185 return( PSA_ERROR_HARDWARE_FAILURE );
186
187 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
188 return( PSA_ERROR_NOT_SUPPORTED );
189 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
190 return( PSA_ERROR_INVALID_ARGUMENT );
191 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
192 return( PSA_ERROR_INSUFFICIENT_MEMORY );
193 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
194 return( PSA_ERROR_INVALID_PADDING );
195 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
196 return( PSA_ERROR_BAD_STATE );
197 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
198 return( PSA_ERROR_INVALID_SIGNATURE );
199 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
200 return( PSA_ERROR_TAMPERING_DETECTED );
201 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
202 return( PSA_ERROR_HARDWARE_FAILURE );
203
204 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
205 return( PSA_ERROR_HARDWARE_FAILURE );
206
207 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
208 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
209 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
210 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
211 return( PSA_ERROR_NOT_SUPPORTED );
212 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
213 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
214
215 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
216 return( PSA_ERROR_NOT_SUPPORTED );
217 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
218 return( PSA_ERROR_HARDWARE_FAILURE );
219
Gilles Peskinee59236f2018-01-27 23:32:46 +0100220 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
221 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
222 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
223 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100224
225 case MBEDTLS_ERR_GCM_AUTH_FAILED:
226 return( PSA_ERROR_INVALID_SIGNATURE );
227 case MBEDTLS_ERR_GCM_BAD_INPUT:
228 return( PSA_ERROR_NOT_SUPPORTED );
229 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
230 return( PSA_ERROR_HARDWARE_FAILURE );
231
232 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
233 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
234 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
235 return( PSA_ERROR_HARDWARE_FAILURE );
236
237 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
238 return( PSA_ERROR_NOT_SUPPORTED );
239 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
240 return( PSA_ERROR_INVALID_ARGUMENT );
241 case MBEDTLS_ERR_MD_ALLOC_FAILED:
242 return( PSA_ERROR_INSUFFICIENT_MEMORY );
243 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
244 return( PSA_ERROR_STORAGE_FAILURE );
245 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
246 return( PSA_ERROR_HARDWARE_FAILURE );
247
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100248 case MBEDTLS_ERR_PK_ALLOC_FAILED:
249 return( PSA_ERROR_INSUFFICIENT_MEMORY );
250 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
251 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
252 return( PSA_ERROR_INVALID_ARGUMENT );
253 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100254 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100255 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
256 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
257 return( PSA_ERROR_INVALID_ARGUMENT );
258 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
259 return( PSA_ERROR_NOT_SUPPORTED );
260 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
261 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
262 return( PSA_ERROR_NOT_PERMITTED );
263 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
264 return( PSA_ERROR_INVALID_ARGUMENT );
265 case MBEDTLS_ERR_PK_INVALID_ALG:
266 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
267 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
268 return( PSA_ERROR_NOT_SUPPORTED );
269 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
270 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100271 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
272 return( PSA_ERROR_HARDWARE_FAILURE );
273
274 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
275 return( PSA_ERROR_HARDWARE_FAILURE );
276
277 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
278 return( PSA_ERROR_INVALID_ARGUMENT );
279 case MBEDTLS_ERR_RSA_INVALID_PADDING:
280 return( PSA_ERROR_INVALID_PADDING );
281 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
282 return( PSA_ERROR_HARDWARE_FAILURE );
283 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
284 return( PSA_ERROR_INVALID_ARGUMENT );
285 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
286 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
287 return( PSA_ERROR_TAMPERING_DETECTED );
288 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
289 return( PSA_ERROR_INVALID_SIGNATURE );
290 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
291 return( PSA_ERROR_BUFFER_TOO_SMALL );
292 case MBEDTLS_ERR_RSA_RNG_FAILED:
293 return( PSA_ERROR_INSUFFICIENT_MEMORY );
294 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
295 return( PSA_ERROR_NOT_SUPPORTED );
296 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
297 return( PSA_ERROR_HARDWARE_FAILURE );
298
299 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
300 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
301 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
302 return( PSA_ERROR_HARDWARE_FAILURE );
303
304 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
305 return( PSA_ERROR_INVALID_ARGUMENT );
306 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
307 return( PSA_ERROR_HARDWARE_FAILURE );
308
itayzafrir5c753392018-05-08 11:18:38 +0300309 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300310 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300311 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300312 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
313 return( PSA_ERROR_BUFFER_TOO_SMALL );
314 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
315 return( PSA_ERROR_NOT_SUPPORTED );
316 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
317 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
318 return( PSA_ERROR_INVALID_SIGNATURE );
319 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
320 return( PSA_ERROR_INSUFFICIENT_MEMORY );
321 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
322 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300323
Gilles Peskinee59236f2018-01-27 23:32:46 +0100324 default:
325 return( PSA_ERROR_UNKNOWN_ERROR );
326 }
327}
328
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200329
330
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100331/****************************************************************/
332/* Key management */
333/****************************************************************/
334
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200335static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
336{
337 switch( grpid )
338 {
339 case MBEDTLS_ECP_DP_SECP192R1:
340 return( PSA_ECC_CURVE_SECP192R1 );
341 case MBEDTLS_ECP_DP_SECP224R1:
342 return( PSA_ECC_CURVE_SECP224R1 );
343 case MBEDTLS_ECP_DP_SECP256R1:
344 return( PSA_ECC_CURVE_SECP256R1 );
345 case MBEDTLS_ECP_DP_SECP384R1:
346 return( PSA_ECC_CURVE_SECP384R1 );
347 case MBEDTLS_ECP_DP_SECP521R1:
348 return( PSA_ECC_CURVE_SECP521R1 );
349 case MBEDTLS_ECP_DP_BP256R1:
350 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
351 case MBEDTLS_ECP_DP_BP384R1:
352 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
353 case MBEDTLS_ECP_DP_BP512R1:
354 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
355 case MBEDTLS_ECP_DP_CURVE25519:
356 return( PSA_ECC_CURVE_CURVE25519 );
357 case MBEDTLS_ECP_DP_SECP192K1:
358 return( PSA_ECC_CURVE_SECP192K1 );
359 case MBEDTLS_ECP_DP_SECP224K1:
360 return( PSA_ECC_CURVE_SECP224K1 );
361 case MBEDTLS_ECP_DP_SECP256K1:
362 return( PSA_ECC_CURVE_SECP256K1 );
363 case MBEDTLS_ECP_DP_CURVE448:
364 return( PSA_ECC_CURVE_CURVE448 );
365 default:
366 return( 0 );
367 }
368}
369
Gilles Peskine12313cd2018-06-20 00:20:32 +0200370static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
371{
372 switch( curve )
373 {
374 case PSA_ECC_CURVE_SECP192R1:
375 return( MBEDTLS_ECP_DP_SECP192R1 );
376 case PSA_ECC_CURVE_SECP224R1:
377 return( MBEDTLS_ECP_DP_SECP224R1 );
378 case PSA_ECC_CURVE_SECP256R1:
379 return( MBEDTLS_ECP_DP_SECP256R1 );
380 case PSA_ECC_CURVE_SECP384R1:
381 return( MBEDTLS_ECP_DP_SECP384R1 );
382 case PSA_ECC_CURVE_SECP521R1:
383 return( MBEDTLS_ECP_DP_SECP521R1 );
384 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
385 return( MBEDTLS_ECP_DP_BP256R1 );
386 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
387 return( MBEDTLS_ECP_DP_BP384R1 );
388 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
389 return( MBEDTLS_ECP_DP_BP512R1 );
390 case PSA_ECC_CURVE_CURVE25519:
391 return( MBEDTLS_ECP_DP_CURVE25519 );
392 case PSA_ECC_CURVE_SECP192K1:
393 return( MBEDTLS_ECP_DP_SECP192K1 );
394 case PSA_ECC_CURVE_SECP224K1:
395 return( MBEDTLS_ECP_DP_SECP224K1 );
396 case PSA_ECC_CURVE_SECP256K1:
397 return( MBEDTLS_ECP_DP_SECP256K1 );
398 case PSA_ECC_CURVE_CURVE448:
399 return( MBEDTLS_ECP_DP_CURVE448 );
400 default:
401 return( MBEDTLS_ECP_DP_NONE );
402 }
403}
404
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200405static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
406 size_t bits,
407 struct raw_data *raw )
408{
409 /* Check that the bit size is acceptable for the key type */
410 switch( type )
411 {
412 case PSA_KEY_TYPE_RAW_DATA:
413#if defined(MBEDTLS_MD_C)
414 case PSA_KEY_TYPE_HMAC:
415#endif
416 break;
417#if defined(MBEDTLS_AES_C)
418 case PSA_KEY_TYPE_AES:
419 if( bits != 128 && bits != 192 && bits != 256 )
420 return( PSA_ERROR_INVALID_ARGUMENT );
421 break;
422#endif
423#if defined(MBEDTLS_CAMELLIA_C)
424 case PSA_KEY_TYPE_CAMELLIA:
425 if( bits != 128 && bits != 192 && bits != 256 )
426 return( PSA_ERROR_INVALID_ARGUMENT );
427 break;
428#endif
429#if defined(MBEDTLS_DES_C)
430 case PSA_KEY_TYPE_DES:
431 if( bits != 64 && bits != 128 && bits != 192 )
432 return( PSA_ERROR_INVALID_ARGUMENT );
433 break;
434#endif
435#if defined(MBEDTLS_ARC4_C)
436 case PSA_KEY_TYPE_ARC4:
437 if( bits < 8 || bits > 2048 )
438 return( PSA_ERROR_INVALID_ARGUMENT );
439 break;
440#endif
441 default:
442 return( PSA_ERROR_NOT_SUPPORTED );
443 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200444 if( bits % 8 != 0 )
445 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200446
447 /* Allocate memory for the key */
448 raw->bytes = PSA_BITS_TO_BYTES( bits );
449 raw->data = mbedtls_calloc( 1, raw->bytes );
450 if( raw->data == NULL )
451 {
452 raw->bytes = 0;
453 return( PSA_ERROR_INSUFFICIENT_MEMORY );
454 }
455 return( PSA_SUCCESS );
456}
457
Gilles Peskine2d277862018-06-18 15:41:12 +0200458psa_status_t psa_import_key( psa_key_slot_t key,
459 psa_key_type_t type,
460 const uint8_t *data,
461 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100462{
463 key_slot_t *slot;
464
Gilles Peskine828ed142018-06-18 23:25:51 +0200465 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100466 return( PSA_ERROR_INVALID_ARGUMENT );
467 slot = &global_data.key_slots[key];
468 if( slot->type != PSA_KEY_TYPE_NONE )
469 return( PSA_ERROR_OCCUPIED_SLOT );
470
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200471 if( key_type_is_raw_bytes( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100472 {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200473 psa_status_t status;
Gilles Peskine6d912132018-03-07 16:41:37 +0100474 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100475 if( data_length > SIZE_MAX / 8 )
476 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200477 status = prepare_raw_data_slot( type,
478 PSA_BYTES_TO_BITS( data_length ),
479 &slot->data.raw );
480 if( status != PSA_SUCCESS )
481 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100482 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100483 }
484 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100485#if defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100486 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
487 type == PSA_KEY_TYPE_RSA_KEYPAIR ||
488 PSA_KEY_TYPE_IS_ECC( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100489 {
490 int ret;
Gilles Peskine969ac722018-01-28 18:16:59 +0100491 mbedtls_pk_context pk;
Gilles Peskinec648d692018-06-28 08:46:13 +0200492 psa_status_t status = PSA_SUCCESS;
Gilles Peskine969ac722018-01-28 18:16:59 +0100493 mbedtls_pk_init( &pk );
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100494 if( PSA_KEY_TYPE_IS_KEYPAIR( type ) )
495 ret = mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 );
496 else
497 ret = mbedtls_pk_parse_public_key( &pk, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100498 if( ret != 0 )
499 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine969ac722018-01-28 18:16:59 +0100500 switch( mbedtls_pk_get_type( &pk ) )
501 {
502#if defined(MBEDTLS_RSA_C)
503 case MBEDTLS_PK_RSA:
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100504 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
505 type == PSA_KEY_TYPE_RSA_KEYPAIR )
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200506 slot->data.rsa = mbedtls_pk_rsa( pk );
Gilles Peskine969ac722018-01-28 18:16:59 +0100507 else
Gilles Peskinec648d692018-06-28 08:46:13 +0200508 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine969ac722018-01-28 18:16:59 +0100509 break;
510#endif /* MBEDTLS_RSA_C */
511#if defined(MBEDTLS_ECP_C)
512 case MBEDTLS_PK_ECKEY:
513 if( PSA_KEY_TYPE_IS_ECC( type ) )
514 {
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200515 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
516 psa_ecc_curve_t actual_curve =
517 mbedtls_ecc_group_to_psa( ecp->grp.id );
518 psa_ecc_curve_t expected_curve =
519 PSA_KEY_TYPE_GET_CURVE( type );
520 if( actual_curve != expected_curve )
Gilles Peskinec648d692018-06-28 08:46:13 +0200521 {
522 status = PSA_ERROR_INVALID_ARGUMENT;
523 break;
524 }
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200525 slot->data.ecp = ecp;
Gilles Peskine969ac722018-01-28 18:16:59 +0100526 }
527 else
Gilles Peskinec648d692018-06-28 08:46:13 +0200528 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine969ac722018-01-28 18:16:59 +0100529 break;
530#endif /* MBEDTLS_ECP_C */
531 default:
Gilles Peskinec648d692018-06-28 08:46:13 +0200532 status = PSA_ERROR_INVALID_ARGUMENT;
533 break;
534 }
535 /* Free the content of the pk object only on error. On success,
536 * the content of the object has been stored in the slot. */
537 if( status != PSA_SUCCESS )
538 {
539 mbedtls_pk_free( &pk );
540 return( status );
Gilles Peskine969ac722018-01-28 18:16:59 +0100541 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100542 }
543 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100544#endif /* defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100545 {
546 return( PSA_ERROR_NOT_SUPPORTED );
547 }
548
549 slot->type = type;
550 return( PSA_SUCCESS );
551}
552
Gilles Peskine2d277862018-06-18 15:41:12 +0200553psa_status_t psa_destroy_key( psa_key_slot_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100554{
555 key_slot_t *slot;
556
Gilles Peskine828ed142018-06-18 23:25:51 +0200557 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100558 return( PSA_ERROR_INVALID_ARGUMENT );
559 slot = &global_data.key_slots[key];
560 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskine154bd952018-04-19 08:38:16 +0200561 {
562 /* No key material to clean, but do zeroize the slot below to wipe
563 * metadata such as policies. */
564 }
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200565 else if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100566 {
567 mbedtls_free( slot->data.raw.data );
568 }
569 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100570#if defined(MBEDTLS_RSA_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100571 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
572 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100573 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100574 mbedtls_rsa_free( slot->data.rsa );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100575 mbedtls_free( slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100576 }
577 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100578#endif /* defined(MBEDTLS_RSA_C) */
579#if defined(MBEDTLS_ECP_C)
580 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
581 {
582 mbedtls_ecp_keypair_free( slot->data.ecp );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100583 mbedtls_free( slot->data.ecp );
Gilles Peskine969ac722018-01-28 18:16:59 +0100584 }
585 else
586#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100587 {
588 /* Shouldn't happen: the key type is not any type that we
Gilles Peskine6d912132018-03-07 16:41:37 +0100589 * put in. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100590 return( PSA_ERROR_TAMPERING_DETECTED );
591 }
592
593 mbedtls_zeroize( slot, sizeof( *slot ) );
594 return( PSA_SUCCESS );
595}
596
Gilles Peskine2d277862018-06-18 15:41:12 +0200597psa_status_t psa_get_key_information( psa_key_slot_t key,
598 psa_key_type_t *type,
599 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100600{
601 key_slot_t *slot;
602
Gilles Peskine828ed142018-06-18 23:25:51 +0200603 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100604 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100605 slot = &global_data.key_slots[key];
606 if( type != NULL )
607 *type = slot->type;
608 if( bits != NULL )
609 *bits = 0;
610 if( slot->type == PSA_KEY_TYPE_NONE )
611 return( PSA_ERROR_EMPTY_SLOT );
612
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200613 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100614 {
615 if( bits != NULL )
616 *bits = slot->data.raw.bytes * 8;
617 }
618 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100619#if defined(MBEDTLS_RSA_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100620 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
621 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100622 {
623 if( bits != NULL )
Gilles Peskine969ac722018-01-28 18:16:59 +0100624 *bits = mbedtls_rsa_get_bitlen( slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100625 }
626 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100627#endif /* defined(MBEDTLS_RSA_C) */
628#if defined(MBEDTLS_ECP_C)
629 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
630 {
631 if( bits != NULL )
632 *bits = slot->data.ecp->grp.pbits;
633 }
634 else
635#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100636 {
637 /* Shouldn't happen: the key type is not any type that we
Gilles Peskine6d912132018-03-07 16:41:37 +0100638 * put in. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100639 return( PSA_ERROR_TAMPERING_DETECTED );
640 }
641
642 return( PSA_SUCCESS );
643}
644
Gilles Peskine2d277862018-06-18 15:41:12 +0200645static psa_status_t psa_internal_export_key( psa_key_slot_t key,
646 uint8_t *data,
647 size_t data_size,
648 size_t *data_length,
649 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100650{
651 key_slot_t *slot;
652
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100653 /* Set the key to empty now, so that even when there are errors, we always
654 * set data_length to a value between 0 and data_size. On error, setting
655 * the key to empty is a good choice because an empty key representation is
656 * unlikely to be accepted anywhere. */
657 *data_length = 0;
658
Gilles Peskine828ed142018-06-18 23:25:51 +0200659 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100660 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100661 slot = &global_data.key_slots[key];
662 if( slot->type == PSA_KEY_TYPE_NONE )
663 return( PSA_ERROR_EMPTY_SLOT );
664
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200665 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300666 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +0300667
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200668 if( ! export_public_key &&
669 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) &&
670 ( slot->policy.usage & PSA_KEY_USAGE_EXPORT ) == 0 )
Moran Peker87567632018-06-04 18:41:37 +0300671 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine2d277862018-06-18 15:41:12 +0200672
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200673 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100674 {
675 if( slot->data.raw.bytes > data_size )
676 return( PSA_ERROR_BUFFER_TOO_SMALL );
677 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
678 *data_length = slot->data.raw.bytes;
679 return( PSA_SUCCESS );
680 }
681 else
Moran Peker17e36e12018-05-02 12:55:20 +0300682 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100683#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100684 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
Moran Pekera998bc62018-04-16 18:16:20 +0300685 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ||
686 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +0100687 {
Moran Pekera998bc62018-04-16 18:16:20 +0300688 mbedtls_pk_context pk;
689 int ret;
690 mbedtls_pk_init( &pk );
691 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
692 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
693 {
694 pk.pk_info = &mbedtls_rsa_info;
695 pk.pk_ctx = slot->data.rsa;
696 }
697 else
698 {
699 pk.pk_info = &mbedtls_eckey_info;
700 pk.pk_ctx = slot->data.ecp;
701 }
Moran Pekerd7326592018-05-29 16:56:39 +0300702 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300703 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +0300704 else
705 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +0300706 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200707 {
708 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +0300709 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200710 }
Gilles Peskine0e231582018-06-20 00:11:07 +0200711 /* The mbedtls_pk_xxx functions write to the end of the buffer.
712 * Move the data to the beginning and erase remaining data
713 * at the original location. */
714 if( 2 * (size_t) ret <= data_size )
715 {
716 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200717 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200718 }
719 else if( (size_t) ret < data_size )
720 {
721 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200722 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200723 }
Moran Pekera998bc62018-04-16 18:16:20 +0300724 *data_length = ret;
725 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +0100726 }
727 else
Gilles Peskine6d912132018-03-07 16:41:37 +0100728#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +0300729 {
730 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +0200731 it is valid for a special-purpose implementation to omit
732 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +0300733 return( PSA_ERROR_NOT_SUPPORTED );
734 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100735 }
736}
737
Gilles Peskine2d277862018-06-18 15:41:12 +0200738psa_status_t psa_export_key( psa_key_slot_t key,
739 uint8_t *data,
740 size_t data_size,
741 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +0300742{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200743 return( psa_internal_export_key( key, data, data_size,
744 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100745}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100746
Gilles Peskine2d277862018-06-18 15:41:12 +0200747psa_status_t psa_export_public_key( psa_key_slot_t key,
748 uint8_t *data,
749 size_t data_size,
750 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +0300751{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200752 return( psa_internal_export_key( key, data, data_size,
753 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +0300754}
755
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200756
757
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100758/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +0100759/* Message digests */
760/****************************************************************/
761
Gilles Peskinedc2fc842018-03-07 16:42:59 +0100762static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +0100763{
764 switch( alg )
765 {
766#if defined(MBEDTLS_MD2_C)
767 case PSA_ALG_MD2:
768 return( &mbedtls_md2_info );
769#endif
770#if defined(MBEDTLS_MD4_C)
771 case PSA_ALG_MD4:
772 return( &mbedtls_md4_info );
773#endif
774#if defined(MBEDTLS_MD5_C)
775 case PSA_ALG_MD5:
776 return( &mbedtls_md5_info );
777#endif
778#if defined(MBEDTLS_RIPEMD160_C)
779 case PSA_ALG_RIPEMD160:
780 return( &mbedtls_ripemd160_info );
781#endif
782#if defined(MBEDTLS_SHA1_C)
783 case PSA_ALG_SHA_1:
784 return( &mbedtls_sha1_info );
785#endif
786#if defined(MBEDTLS_SHA256_C)
787 case PSA_ALG_SHA_224:
788 return( &mbedtls_sha224_info );
789 case PSA_ALG_SHA_256:
790 return( &mbedtls_sha256_info );
791#endif
792#if defined(MBEDTLS_SHA512_C)
793 case PSA_ALG_SHA_384:
794 return( &mbedtls_sha384_info );
795 case PSA_ALG_SHA_512:
796 return( &mbedtls_sha512_info );
797#endif
798 default:
799 return( NULL );
800 }
801}
802
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100803psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
804{
805 switch( operation->alg )
806 {
Gilles Peskine81736312018-06-26 15:04:31 +0200807 case 0:
808 /* The object has (apparently) been initialized but it is not
809 * in use. It's ok to call abort on such an object, and there's
810 * nothing to do. */
811 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100812#if defined(MBEDTLS_MD2_C)
813 case PSA_ALG_MD2:
814 mbedtls_md2_free( &operation->ctx.md2 );
815 break;
816#endif
817#if defined(MBEDTLS_MD4_C)
818 case PSA_ALG_MD4:
819 mbedtls_md4_free( &operation->ctx.md4 );
820 break;
821#endif
822#if defined(MBEDTLS_MD5_C)
823 case PSA_ALG_MD5:
824 mbedtls_md5_free( &operation->ctx.md5 );
825 break;
826#endif
827#if defined(MBEDTLS_RIPEMD160_C)
828 case PSA_ALG_RIPEMD160:
829 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
830 break;
831#endif
832#if defined(MBEDTLS_SHA1_C)
833 case PSA_ALG_SHA_1:
834 mbedtls_sha1_free( &operation->ctx.sha1 );
835 break;
836#endif
837#if defined(MBEDTLS_SHA256_C)
838 case PSA_ALG_SHA_224:
839 case PSA_ALG_SHA_256:
840 mbedtls_sha256_free( &operation->ctx.sha256 );
841 break;
842#endif
843#if defined(MBEDTLS_SHA512_C)
844 case PSA_ALG_SHA_384:
845 case PSA_ALG_SHA_512:
846 mbedtls_sha512_free( &operation->ctx.sha512 );
847 break;
848#endif
849 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +0200850 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100851 }
852 operation->alg = 0;
853 return( PSA_SUCCESS );
854}
855
856psa_status_t psa_hash_start( psa_hash_operation_t *operation,
857 psa_algorithm_t alg )
858{
859 int ret;
860 operation->alg = 0;
861 switch( alg )
862 {
863#if defined(MBEDTLS_MD2_C)
864 case PSA_ALG_MD2:
865 mbedtls_md2_init( &operation->ctx.md2 );
866 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
867 break;
868#endif
869#if defined(MBEDTLS_MD4_C)
870 case PSA_ALG_MD4:
871 mbedtls_md4_init( &operation->ctx.md4 );
872 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
873 break;
874#endif
875#if defined(MBEDTLS_MD5_C)
876 case PSA_ALG_MD5:
877 mbedtls_md5_init( &operation->ctx.md5 );
878 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
879 break;
880#endif
881#if defined(MBEDTLS_RIPEMD160_C)
882 case PSA_ALG_RIPEMD160:
883 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
884 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
885 break;
886#endif
887#if defined(MBEDTLS_SHA1_C)
888 case PSA_ALG_SHA_1:
889 mbedtls_sha1_init( &operation->ctx.sha1 );
890 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
891 break;
892#endif
893#if defined(MBEDTLS_SHA256_C)
894 case PSA_ALG_SHA_224:
895 mbedtls_sha256_init( &operation->ctx.sha256 );
896 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
897 break;
898 case PSA_ALG_SHA_256:
899 mbedtls_sha256_init( &operation->ctx.sha256 );
900 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
901 break;
902#endif
903#if defined(MBEDTLS_SHA512_C)
904 case PSA_ALG_SHA_384:
905 mbedtls_sha512_init( &operation->ctx.sha512 );
906 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
907 break;
908 case PSA_ALG_SHA_512:
909 mbedtls_sha512_init( &operation->ctx.sha512 );
910 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
911 break;
912#endif
913 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +0200914 return( PSA_ALG_IS_HASH( alg ) ?
915 PSA_ERROR_NOT_SUPPORTED :
916 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100917 }
918 if( ret == 0 )
919 operation->alg = alg;
920 else
921 psa_hash_abort( operation );
922 return( mbedtls_to_psa_error( ret ) );
923}
924
925psa_status_t psa_hash_update( psa_hash_operation_t *operation,
926 const uint8_t *input,
927 size_t input_length )
928{
929 int ret;
930 switch( operation->alg )
931 {
932#if defined(MBEDTLS_MD2_C)
933 case PSA_ALG_MD2:
934 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
935 input, input_length );
936 break;
937#endif
938#if defined(MBEDTLS_MD4_C)
939 case PSA_ALG_MD4:
940 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
941 input, input_length );
942 break;
943#endif
944#if defined(MBEDTLS_MD5_C)
945 case PSA_ALG_MD5:
946 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
947 input, input_length );
948 break;
949#endif
950#if defined(MBEDTLS_RIPEMD160_C)
951 case PSA_ALG_RIPEMD160:
952 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
953 input, input_length );
954 break;
955#endif
956#if defined(MBEDTLS_SHA1_C)
957 case PSA_ALG_SHA_1:
958 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
959 input, input_length );
960 break;
961#endif
962#if defined(MBEDTLS_SHA256_C)
963 case PSA_ALG_SHA_224:
964 case PSA_ALG_SHA_256:
965 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
966 input, input_length );
967 break;
968#endif
969#if defined(MBEDTLS_SHA512_C)
970 case PSA_ALG_SHA_384:
971 case PSA_ALG_SHA_512:
972 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
973 input, input_length );
974 break;
975#endif
976 default:
977 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
978 break;
979 }
980 if( ret != 0 )
981 psa_hash_abort( operation );
982 return( mbedtls_to_psa_error( ret ) );
983}
984
985psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
986 uint8_t *hash,
987 size_t hash_size,
988 size_t *hash_length )
989{
990 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +0200991 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100992
993 /* Fill the output buffer with something that isn't a valid hash
994 * (barring an attack on the hash and deliberately-crafted input),
995 * in case the caller doesn't check the return status properly. */
996 *hash_length = actual_hash_length;
997 memset( hash, '!', hash_size );
998
999 if( hash_size < actual_hash_length )
1000 return( PSA_ERROR_BUFFER_TOO_SMALL );
1001
1002 switch( operation->alg )
1003 {
1004#if defined(MBEDTLS_MD2_C)
1005 case PSA_ALG_MD2:
1006 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1007 break;
1008#endif
1009#if defined(MBEDTLS_MD4_C)
1010 case PSA_ALG_MD4:
1011 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1012 break;
1013#endif
1014#if defined(MBEDTLS_MD5_C)
1015 case PSA_ALG_MD5:
1016 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1017 break;
1018#endif
1019#if defined(MBEDTLS_RIPEMD160_C)
1020 case PSA_ALG_RIPEMD160:
1021 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1022 break;
1023#endif
1024#if defined(MBEDTLS_SHA1_C)
1025 case PSA_ALG_SHA_1:
1026 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1027 break;
1028#endif
1029#if defined(MBEDTLS_SHA256_C)
1030 case PSA_ALG_SHA_224:
1031 case PSA_ALG_SHA_256:
1032 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1033 break;
1034#endif
1035#if defined(MBEDTLS_SHA512_C)
1036 case PSA_ALG_SHA_384:
1037 case PSA_ALG_SHA_512:
1038 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1039 break;
1040#endif
1041 default:
1042 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1043 break;
1044 }
1045
1046 if( ret == 0 )
1047 {
1048 return( psa_hash_abort( operation ) );
1049 }
1050 else
1051 {
1052 psa_hash_abort( operation );
1053 return( mbedtls_to_psa_error( ret ) );
1054 }
1055}
1056
Gilles Peskine2d277862018-06-18 15:41:12 +02001057psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1058 const uint8_t *hash,
1059 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001060{
1061 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1062 size_t actual_hash_length;
1063 psa_status_t status = psa_hash_finish( operation,
1064 actual_hash, sizeof( actual_hash ),
1065 &actual_hash_length );
1066 if( status != PSA_SUCCESS )
1067 return( status );
1068 if( actual_hash_length != hash_length )
1069 return( PSA_ERROR_INVALID_SIGNATURE );
1070 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1071 return( PSA_ERROR_INVALID_SIGNATURE );
1072 return( PSA_SUCCESS );
1073}
1074
1075
1076
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001077/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001078/* MAC */
1079/****************************************************************/
1080
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001081static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001082 psa_algorithm_t alg,
1083 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001084 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001085 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001086{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001087 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001088 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001089
1090 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1091 {
1092 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) )
mohammad16038481e742018-03-18 13:57:31 +02001093 {
1094 alg &= ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
1095 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001096
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001097 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001098 {
1099 case PSA_ALG_STREAM_CIPHER:
1100 mode = MBEDTLS_MODE_STREAM;
1101 break;
1102 case PSA_ALG_CBC_BASE:
1103 mode = MBEDTLS_MODE_CBC;
1104 break;
1105 case PSA_ALG_CFB_BASE:
1106 mode = MBEDTLS_MODE_CFB;
1107 break;
1108 case PSA_ALG_OFB_BASE:
1109 mode = MBEDTLS_MODE_OFB;
1110 break;
1111 case PSA_ALG_CTR:
1112 mode = MBEDTLS_MODE_CTR;
1113 break;
1114 case PSA_ALG_CCM:
1115 mode = MBEDTLS_MODE_CCM;
1116 break;
1117 case PSA_ALG_GCM:
1118 mode = MBEDTLS_MODE_GCM;
1119 break;
1120 default:
1121 return( NULL );
1122 }
1123 }
1124 else if( alg == PSA_ALG_CMAC )
1125 mode = MBEDTLS_MODE_ECB;
1126 else if( alg == PSA_ALG_GMAC )
1127 mode = MBEDTLS_MODE_GCM;
1128 else
1129 return( NULL );
1130
1131 switch( key_type )
1132 {
1133 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001134 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001135 break;
1136 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001137 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1138 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001139 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001140 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001141 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001142 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001143 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1144 * but two-key Triple-DES is functionally three-key Triple-DES
1145 * with K1=K3, so that's how we present it to mbedtls. */
1146 if( key_bits == 128 )
1147 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001148 break;
1149 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001150 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001151 break;
1152 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001153 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001154 break;
1155 default:
1156 return( NULL );
1157 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001158 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001159 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001160
mohammad1603f4f0d612018-06-03 15:04:51 +03001161 return( mbedtls_cipher_info_from_values( cipher_id_tmp, key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001162}
1163
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001164static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001165{
Gilles Peskine2d277862018-06-18 15:41:12 +02001166 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001167 {
1168 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001169 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001170 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001171 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001172 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001173 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001174 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001175 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001176 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001177 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001178 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001179 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001180 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001181 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001182 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001183 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001184 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001185 return( 128 );
1186 default:
1187 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001188 }
1189}
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001190
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001191/* Initialize the MAC operation structure. Once this function has been
1192 * called, psa_mac_abort can run and will do the right thing. */
1193static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1194 psa_algorithm_t alg )
1195{
1196 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1197
1198 operation->alg = alg;
1199 operation->key_set = 0;
1200 operation->iv_set = 0;
1201 operation->iv_required = 0;
1202 operation->has_input = 0;
1203 operation->key_usage_sign = 0;
1204 operation->key_usage_verify = 0;
1205
1206#if defined(MBEDTLS_CMAC_C)
1207 if( alg == PSA_ALG_CMAC )
1208 {
1209 operation->iv_required = 0;
1210 mbedtls_cipher_init( &operation->ctx.cmac );
1211 status = PSA_SUCCESS;
1212 }
1213 else
1214#endif /* MBEDTLS_CMAC_C */
1215#if defined(MBEDTLS_MD_C)
1216 if( PSA_ALG_IS_HMAC( operation->alg ) )
1217 {
1218 status = psa_hash_start( &operation->ctx.hmac.hash_ctx,
1219 PSA_ALG_HMAC_HASH( alg ) );
1220 }
1221 else
1222#endif /* MBEDTLS_MD_C */
1223 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001224 if( ! PSA_ALG_IS_MAC( alg ) )
1225 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001226 }
1227
1228 if( status != PSA_SUCCESS )
1229 memset( operation, 0, sizeof( *operation ) );
1230 return( status );
1231}
1232
Gilles Peskine8c9def32018-02-08 10:02:12 +01001233psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1234{
1235 switch( operation->alg )
1236 {
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001237 case 0:
Gilles Peskine81736312018-06-26 15:04:31 +02001238 /* The object has (apparently) been initialized but it is not
1239 * in use. It's ok to call abort on such an object, and there's
1240 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001241 return( PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001242#if defined(MBEDTLS_CMAC_C)
1243 case PSA_ALG_CMAC:
1244 mbedtls_cipher_free( &operation->ctx.cmac );
1245 break;
1246#endif /* MBEDTLS_CMAC_C */
1247 default:
1248#if defined(MBEDTLS_MD_C)
1249 if( PSA_ALG_IS_HMAC( operation->alg ) )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001250 {
Gilles Peskine99bc6492018-06-11 17:13:00 +02001251 unsigned int block_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001252 psa_get_hash_block_size( PSA_ALG_HMAC_HASH( operation->alg ) );
Nir Sonnenschein084832d2018-06-08 22:52:24 +03001253
Gilles Peskine99bc6492018-06-11 17:13:00 +02001254 if( block_size == 0 )
Nir Sonnenschein084832d2018-06-08 22:52:24 +03001255 return( PSA_ERROR_NOT_SUPPORTED );
1256
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001257 psa_hash_abort( &operation->ctx.hmac.hash_ctx );
Gilles Peskine2d277862018-06-18 15:41:12 +02001258 mbedtls_zeroize( operation->ctx.hmac.opad, block_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001259 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001260 else
1261#endif /* MBEDTLS_MD_C */
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001262 {
1263 /* Sanity check (shouldn't happen: operation->alg should
1264 * always have been initialized to a valid value). */
1265 return( PSA_ERROR_BAD_STATE );
1266 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001267 }
Moran Peker41deec42018-04-04 15:43:05 +03001268
Gilles Peskine8c9def32018-02-08 10:02:12 +01001269 operation->alg = 0;
1270 operation->key_set = 0;
1271 operation->iv_set = 0;
1272 operation->iv_required = 0;
1273 operation->has_input = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001274 operation->key_usage_sign = 0;
1275 operation->key_usage_verify = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001276
Gilles Peskine8c9def32018-02-08 10:02:12 +01001277 return( PSA_SUCCESS );
1278}
1279
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001280#if defined(MBEDTLS_CMAC_C)
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001281static int psa_cmac_start( psa_mac_operation_t *operation,
1282 size_t key_bits,
1283 key_slot_t *slot,
1284 const mbedtls_cipher_info_t *cipher_info )
1285{
1286 int ret;
1287
1288 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001289
1290 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1291 if( ret != 0 )
1292 return( ret );
1293
1294 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1295 slot->data.raw.data,
1296 key_bits );
1297 return( ret );
1298}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001299#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001300
Gilles Peskine248051a2018-06-20 16:09:38 +02001301#if defined(MBEDTLS_MD_C)
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001302static int psa_hmac_start( psa_mac_operation_t *operation,
1303 psa_key_type_t key_type,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001304 key_slot_t *slot,
1305 psa_algorithm_t alg )
1306{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001307 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Nir Sonnenschein5ca65472018-06-17 14:03:40 +03001308 unsigned char *opad = operation->ctx.hmac.opad;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001309 size_t i;
Gilles Peskinee1bc6802018-06-11 17:36:05 +02001310 size_t block_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001311 psa_get_hash_block_size( PSA_ALG_HMAC_HASH( alg ) );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001312 unsigned int digest_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001313 PSA_HASH_SIZE( PSA_ALG_HMAC_HASH( alg ) );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001314 size_t key_length = slot->data.raw.bytes;
1315 psa_status_t status;
1316
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001317 if( block_size == 0 || digest_size == 0 )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001318 return( PSA_ERROR_NOT_SUPPORTED );
1319
1320 if( key_type != PSA_KEY_TYPE_HMAC )
1321 return( PSA_ERROR_INVALID_ARGUMENT );
1322
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001323 operation->mac_size = digest_size;
1324
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001325 /* The hash was started earlier in psa_mac_init. */
Gilles Peskined223b522018-06-11 18:12:58 +02001326 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001327 {
1328 status = psa_hash_update( &operation->ctx.hmac.hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001329 slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001330 if( status != PSA_SUCCESS )
1331 return( status );
1332 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001333 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001334 if( status != PSA_SUCCESS )
1335 return( status );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001336 }
Gilles Peskined223b522018-06-11 18:12:58 +02001337 else
1338 memcpy( ipad, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001339
Gilles Peskined223b522018-06-11 18:12:58 +02001340 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1341 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001342 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001343 ipad[i] ^= 0x36;
1344 memset( ipad + key_length, 0x36, block_size - key_length );
1345
1346 /* Copy the key material from ipad to opad, flipping the requisite bits,
1347 * and filling the rest of opad with the requisite constant. */
1348 for( i = 0; i < key_length; i++ )
1349 opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1350 memset( opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001351
1352 status = psa_hash_start( &operation->ctx.hmac.hash_ctx,
1353 PSA_ALG_HMAC_HASH( alg ) );
1354 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001355 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001356
1357 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, ipad,
1358 block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001359
1360cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001361 mbedtls_zeroize( ipad, key_length );
1362 /* opad is in the context. It needs to stay in memory if this function
1363 * succeeds, and it will be wiped by psa_mac_abort() called from
1364 * psa_mac_start in the error case. */
1365
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001366 return( status );
1367}
Gilles Peskine248051a2018-06-20 16:09:38 +02001368#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001369
Gilles Peskine8c9def32018-02-08 10:02:12 +01001370psa_status_t psa_mac_start( psa_mac_operation_t *operation,
1371 psa_key_slot_t key,
1372 psa_algorithm_t alg )
1373{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001374 psa_status_t status;
1375 key_slot_t *slot;
1376 psa_key_type_t key_type;
1377 size_t key_bits;
Gilles Peskine828ed142018-06-18 23:25:51 +02001378 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001379
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001380 status = psa_mac_init( operation, alg );
1381 if( status != PSA_SUCCESS )
1382 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001383
1384 status = psa_get_key_information( key, &key_type, &key_bits );
1385 if( status != PSA_SUCCESS )
1386 return( status );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001387
Gilles Peskine8c9def32018-02-08 10:02:12 +01001388 slot = &global_data.key_slots[key];
Gilles Peskine99bc6492018-06-11 17:13:00 +02001389 if( slot->type == PSA_KEY_TYPE_NONE )
1390 return( PSA_ERROR_EMPTY_SLOT );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001391
Moran Pekerd7326592018-05-29 16:56:39 +03001392 if( ( slot->policy.usage & PSA_KEY_USAGE_SIGN ) != 0 )
mohammad16036df908f2018-04-02 08:34:15 -07001393 operation->key_usage_sign = 1;
1394
Moran Pekerd7326592018-05-29 16:56:39 +03001395 if( ( slot->policy.usage & PSA_KEY_USAGE_VERIFY ) != 0 )
mohammad16036df908f2018-04-02 08:34:15 -07001396 operation->key_usage_verify = 1;
1397
Gilles Peskine8c9def32018-02-08 10:02:12 +01001398 if( ! PSA_ALG_IS_HMAC( alg ) )
1399 {
mohammad1603f4f0d612018-06-03 15:04:51 +03001400 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, key_bits, NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001401 if( cipher_info == NULL )
1402 return( PSA_ERROR_NOT_SUPPORTED );
1403 operation->mac_size = cipher_info->block_size;
1404 }
1405 switch( alg )
1406 {
1407#if defined(MBEDTLS_CMAC_C)
1408 case PSA_ALG_CMAC:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001409 status = mbedtls_to_psa_error( psa_cmac_start( operation,
1410 key_bits,
1411 slot,
1412 cipher_info ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001413 break;
1414#endif /* MBEDTLS_CMAC_C */
1415 default:
1416#if defined(MBEDTLS_MD_C)
1417 if( PSA_ALG_IS_HMAC( alg ) )
Gilles Peskined223b522018-06-11 18:12:58 +02001418 status = psa_hmac_start( operation, key_type, slot, alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001419 else
1420#endif /* MBEDTLS_MD_C */
1421 return( PSA_ERROR_NOT_SUPPORTED );
1422 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001423
Gilles Peskine8c9def32018-02-08 10:02:12 +01001424 /* If we reach this point, then the algorithm-specific part of the
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001425 * context may contain data that needs to be wiped on error. */
1426 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001427 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001428 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001429 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001430 else
1431 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001432 operation->key_set = 1;
1433 }
1434 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001435}
1436
1437psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1438 const uint8_t *input,
1439 size_t input_length )
1440{
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001441 int ret = 0 ;
1442 psa_status_t status = PSA_SUCCESS;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001443 if( ! operation->key_set )
1444 return( PSA_ERROR_BAD_STATE );
1445 if( operation->iv_required && ! operation->iv_set )
1446 return( PSA_ERROR_BAD_STATE );
1447 operation->has_input = 1;
1448
1449 switch( operation->alg )
1450 {
1451#if defined(MBEDTLS_CMAC_C)
1452 case PSA_ALG_CMAC:
1453 ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1454 input, input_length );
1455 break;
1456#endif /* MBEDTLS_CMAC_C */
1457 default:
1458#if defined(MBEDTLS_MD_C)
1459 if( PSA_ALG_IS_HMAC( operation->alg ) )
1460 {
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001461 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
Gilles Peskine2d277862018-06-18 15:41:12 +02001462 input_length );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001463 }
1464 else
1465#endif /* MBEDTLS_MD_C */
1466 {
1467 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1468 }
1469 break;
1470 }
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001471 if( ret != 0 || status != PSA_SUCCESS )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001472 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001473 psa_mac_abort( operation );
Gilles Peskine2d277862018-06-18 15:41:12 +02001474 if( ret != 0 )
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001475 status = mbedtls_to_psa_error( ret );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001476 }
1477
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001478 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001479}
1480
mohammad16036df908f2018-04-02 08:34:15 -07001481static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02001482 uint8_t *mac,
1483 size_t mac_size,
1484 size_t *mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001485{
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001486 int ret = 0;
1487 psa_status_t status = PSA_SUCCESS;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001488 if( ! operation->key_set )
1489 return( PSA_ERROR_BAD_STATE );
1490 if( operation->iv_required && ! operation->iv_set )
1491 return( PSA_ERROR_BAD_STATE );
1492
1493 /* Fill the output buffer with something that isn't a valid mac
1494 * (barring an attack on the mac and deliberately-crafted input),
1495 * in case the caller doesn't check the return status properly. */
1496 *mac_length = operation->mac_size;
1497 memset( mac, '!', mac_size );
1498
1499 if( mac_size < operation->mac_size )
1500 return( PSA_ERROR_BUFFER_TOO_SMALL );
1501
1502 switch( operation->alg )
1503 {
1504#if defined(MBEDTLS_CMAC_C)
1505 case PSA_ALG_CMAC:
1506 ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, mac );
1507 break;
1508#endif /* MBEDTLS_CMAC_C */
1509 default:
1510#if defined(MBEDTLS_MD_C)
1511 if( PSA_ALG_IS_HMAC( operation->alg ) )
1512 {
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001513 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
Nir Sonnenschein5ca65472018-06-17 14:03:40 +03001514 unsigned char *opad = operation->ctx.hmac.opad;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001515 size_t hash_size = 0;
Nir Sonnenschein96272412018-06-17 14:41:10 +03001516 size_t block_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001517 psa_get_hash_block_size( PSA_ALG_HMAC_HASH( operation->alg ) );
Nir Sonnenschein084832d2018-06-08 22:52:24 +03001518
Gilles Peskine99bc6492018-06-11 17:13:00 +02001519 if( block_size == 0 )
1520 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001521
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001522 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx, tmp,
Gilles Peskine99bc6492018-06-11 17:13:00 +02001523 sizeof( tmp ), &hash_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001524 if( status != PSA_SUCCESS )
1525 goto cleanup;
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001526 /* From here on, tmp needs to be wiped. */
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001527
1528 status = psa_hash_start( &operation->ctx.hmac.hash_ctx,
1529 PSA_ALG_HMAC_HASH( operation->alg ) );
1530 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001531 goto hmac_cleanup;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001532
1533 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, opad,
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001534 block_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001535 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001536 goto hmac_cleanup;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001537
1538 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, tmp,
Gilles Peskine2d277862018-06-18 15:41:12 +02001539 hash_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001540 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001541 goto hmac_cleanup;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001542
1543 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx, mac,
1544 mac_size, mac_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001545 hmac_cleanup:
1546 mbedtls_zeroize( tmp, hash_size );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001547 }
1548 else
1549#endif /* MBEDTLS_MD_C */
1550 {
1551 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1552 }
1553 break;
1554 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001555cleanup:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001556
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001557 if( ret == 0 && status == PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001558 {
1559 return( psa_mac_abort( operation ) );
1560 }
1561 else
1562 {
1563 psa_mac_abort( operation );
Gilles Peskine99bc6492018-06-11 17:13:00 +02001564 if( ret != 0 )
Gilles Peskine2d277862018-06-18 15:41:12 +02001565 status = mbedtls_to_psa_error( ret );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001566
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001567 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001568 }
1569}
1570
mohammad16036df908f2018-04-02 08:34:15 -07001571psa_status_t psa_mac_finish( psa_mac_operation_t *operation,
1572 uint8_t *mac,
1573 size_t mac_size,
1574 size_t *mac_length )
1575{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001576 if( ! operation->key_usage_sign )
mohammad16036df908f2018-04-02 08:34:15 -07001577 return( PSA_ERROR_NOT_PERMITTED );
1578
Gilles Peskine99bc6492018-06-11 17:13:00 +02001579 return( psa_mac_finish_internal( operation, mac,
1580 mac_size, mac_length ) );
mohammad16036df908f2018-04-02 08:34:15 -07001581}
1582
Gilles Peskine828ed142018-06-18 23:25:51 +02001583#define PSA_MAC_MAX_SIZE \
Gilles Peskine2d277862018-06-18 15:41:12 +02001584 ( MBEDTLS_MD_MAX_SIZE > MBEDTLS_MAX_BLOCK_LENGTH ? \
1585 MBEDTLS_MD_MAX_SIZE : \
Gilles Peskine8c9def32018-02-08 10:02:12 +01001586 MBEDTLS_MAX_BLOCK_LENGTH )
1587psa_status_t psa_mac_verify( psa_mac_operation_t *operation,
1588 const uint8_t *mac,
1589 size_t mac_length )
1590{
Gilles Peskine828ed142018-06-18 23:25:51 +02001591 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
Gilles Peskine8c9def32018-02-08 10:02:12 +01001592 size_t actual_mac_length;
mohammad16036df908f2018-04-02 08:34:15 -07001593 psa_status_t status;
1594
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001595 if( ! operation->key_usage_verify )
mohammad16036df908f2018-04-02 08:34:15 -07001596 return( PSA_ERROR_NOT_PERMITTED );
1597
1598 status = psa_mac_finish_internal( operation,
1599 actual_mac, sizeof( actual_mac ),
1600 &actual_mac_length );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001601 if( status != PSA_SUCCESS )
1602 return( status );
1603 if( actual_mac_length != mac_length )
1604 return( PSA_ERROR_INVALID_SIGNATURE );
1605 if( safer_memcmp( mac, actual_mac, actual_mac_length ) != 0 )
1606 return( PSA_ERROR_INVALID_SIGNATURE );
1607 return( PSA_SUCCESS );
1608}
1609
1610
Gilles Peskine20035e32018-02-03 22:44:14 +01001611
Gilles Peskine20035e32018-02-03 22:44:14 +01001612/****************************************************************/
1613/* Asymmetric cryptography */
1614/****************************************************************/
1615
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001616/* Decode the hash algorithm from alg and store the mbedtls encoding in
1617 * md_alg. Verify that the hash length is consistent. */
1618static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
1619 size_t hash_length,
1620 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001621{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001622 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001623 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
1624 *md_alg = hash_alg == 0 ? MBEDTLS_MD_NONE : mbedtls_md_get_type( md_info );
1625 if( *md_alg == MBEDTLS_MD_NONE )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001626 {
1627#if SIZE_MAX > UINT_MAX
Gilles Peskine61b91d42018-06-08 16:09:36 +02001628 if( hash_length > UINT_MAX )
1629 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001630#endif
1631 }
1632 else
1633 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001634 if( mbedtls_md_get_size( md_info ) != hash_length )
1635 return( PSA_ERROR_INVALID_ARGUMENT );
1636 if( md_info == NULL )
1637 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001638 }
Gilles Peskine61b91d42018-06-08 16:09:36 +02001639 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001640}
1641
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001642#if defined(MBEDTLS_ECDSA_C)
1643/* Temporary copy from ecdsa.c */
1644static int ecdsa_signature_to_asn1( const mbedtls_mpi *r, const mbedtls_mpi *s,
1645 unsigned char *sig, size_t *slen )
1646{
1647 int ret;
1648 unsigned char buf[MBEDTLS_ECDSA_MAX_LEN];
1649 unsigned char *p = buf + sizeof( buf );
1650 size_t len = 0;
1651
1652 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &p, buf, s ) );
1653 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &p, buf, r ) );
1654
1655 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &p, buf, len ) );
1656 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &p, buf,
1657 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
1658
1659 memcpy( sig, p, len );
1660 *slen = len;
1661
1662 return( 0 );
1663}
1664
1665/* `ecp` cannot be const because `ecp->grp` needs to be non-const
1666 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
1667 * (even though these functions don't modify it). */
1668static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
1669 psa_algorithm_t alg,
1670 const uint8_t *hash,
1671 size_t hash_length,
1672 uint8_t *signature,
1673 size_t signature_size,
1674 size_t *signature_length )
1675{
1676 int ret;
1677 mbedtls_mpi r, s;
1678 mbedtls_mpi_init( &r );
1679 mbedtls_mpi_init( &s );
1680
1681 if( signature_size < PSA_ECDSA_SIGNATURE_SIZE( ecp->grp.pbits ) )
1682 return( PSA_ERROR_BUFFER_TOO_SMALL );
1683
1684 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
1685 {
1686 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
1687 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
1688 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
1689 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
1690 hash, hash_length,
1691 md_alg ) );
1692 }
1693 else
1694 {
1695 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
1696 hash, hash_length,
1697 mbedtls_ctr_drbg_random,
1698 &global_data.ctr_drbg ) );
1699 }
1700 MBEDTLS_MPI_CHK( ecdsa_signature_to_asn1( &r, &s,
1701 signature, signature_length ) );
1702
1703cleanup:
1704 mbedtls_mpi_free( &r );
1705 mbedtls_mpi_free( &s );
1706 return( mbedtls_to_psa_error( ret ) );
1707}
1708#endif /* MBEDTLS_ECDSA_C */
1709
Gilles Peskine61b91d42018-06-08 16:09:36 +02001710psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
1711 psa_algorithm_t alg,
1712 const uint8_t *hash,
1713 size_t hash_length,
1714 const uint8_t *salt,
1715 size_t salt_length,
1716 uint8_t *signature,
1717 size_t signature_size,
1718 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01001719{
1720 key_slot_t *slot;
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001721 psa_status_t status;
Gilles Peskine93aa0332018-02-03 23:58:03 +01001722 *signature_length = 0;
1723 (void) salt;
1724 (void) salt_length;
1725
Gilles Peskine828ed142018-06-18 23:25:51 +02001726 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Gilles Peskine20035e32018-02-03 22:44:14 +01001727 return( PSA_ERROR_EMPTY_SLOT );
1728 slot = &global_data.key_slots[key];
1729 if( slot->type == PSA_KEY_TYPE_NONE )
1730 return( PSA_ERROR_EMPTY_SLOT );
1731 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
1732 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001733 if( ! ( slot->policy.usage & PSA_KEY_USAGE_SIGN ) )
mohammad160306e79202018-03-28 13:17:44 +03001734 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine20035e32018-02-03 22:44:14 +01001735
Gilles Peskine20035e32018-02-03 22:44:14 +01001736#if defined(MBEDTLS_RSA_C)
1737 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
1738 {
1739 mbedtls_rsa_context *rsa = slot->data.rsa;
1740 int ret;
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001741 mbedtls_md_type_t md_alg;
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001742 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001743 if( status != PSA_SUCCESS )
1744 return( status );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001745
Gilles Peskine20035e32018-02-03 22:44:14 +01001746 if( signature_size < rsa->len )
1747 return( PSA_ERROR_BUFFER_TOO_SMALL );
1748#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskinea5926232018-03-28 14:16:50 +02001749 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01001750 {
1751 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1752 MBEDTLS_MD_NONE );
1753 ret = mbedtls_rsa_pkcs1_sign( rsa,
1754 mbedtls_ctr_drbg_random,
1755 &global_data.ctr_drbg,
1756 MBEDTLS_RSA_PRIVATE,
1757 md_alg, hash_length, hash,
1758 signature );
1759 }
1760 else
1761#endif /* MBEDTLS_PKCS1_V15 */
1762#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02001763 if( PSA_ALG_IS_RSA_PSS( alg ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01001764 {
1765 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1766 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
1767 mbedtls_ctr_drbg_random,
1768 &global_data.ctr_drbg,
1769 MBEDTLS_RSA_PRIVATE,
1770 md_alg, hash_length, hash,
1771 signature );
1772 }
1773 else
1774#endif /* MBEDTLS_PKCS1_V21 */
1775 {
1776 return( PSA_ERROR_INVALID_ARGUMENT );
1777 }
Gilles Peskine93aa0332018-02-03 23:58:03 +01001778 if( ret == 0 )
1779 *signature_length = rsa->len;
Gilles Peskine20035e32018-02-03 22:44:14 +01001780 return( mbedtls_to_psa_error( ret ) );
1781 }
1782 else
1783#endif /* defined(MBEDTLS_RSA_C) */
1784#if defined(MBEDTLS_ECP_C)
1785 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1786 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001787#if defined(MBEDTLS_ECDSA_C)
1788 if( PSA_ALG_IS_ECDSA( alg ) )
1789 status = psa_ecdsa_sign( slot->data.ecp,
1790 alg,
1791 hash, hash_length,
1792 signature, signature_size,
1793 signature_length );
1794 else
1795#endif /* defined(MBEDTLS_ECDSA_C) */
1796 {
1797 return( PSA_ERROR_INVALID_ARGUMENT );
1798 }
1799 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03001800 }
1801 else
1802#endif /* defined(MBEDTLS_ECP_C) */
1803 {
Gilles Peskine20035e32018-02-03 22:44:14 +01001804 return( PSA_ERROR_NOT_SUPPORTED );
1805 }
itayzafrir5c753392018-05-08 11:18:38 +03001806}
1807
1808psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
1809 psa_algorithm_t alg,
1810 const uint8_t *hash,
1811 size_t hash_length,
1812 const uint8_t *salt,
1813 size_t salt_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02001814 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02001815 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03001816{
1817 key_slot_t *slot;
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001818 psa_status_t status;
itayzafrir5c753392018-05-08 11:18:38 +03001819 (void) salt;
1820 (void) salt_length;
1821
Gilles Peskine828ed142018-06-18 23:25:51 +02001822 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
itayzafrir5c753392018-05-08 11:18:38 +03001823 return( PSA_ERROR_INVALID_ARGUMENT );
1824 slot = &global_data.key_slots[key];
1825 if( slot->type == PSA_KEY_TYPE_NONE )
1826 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001827 if( ! ( slot->policy.usage & PSA_KEY_USAGE_VERIFY ) )
itayzafrir5c753392018-05-08 11:18:38 +03001828 return( PSA_ERROR_NOT_PERMITTED );
1829
Gilles Peskine61b91d42018-06-08 16:09:36 +02001830#if defined(MBEDTLS_RSA_C)
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001831 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ||
1832 slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001833 {
1834 mbedtls_rsa_context *rsa = slot->data.rsa;
1835 int ret;
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001836 mbedtls_md_type_t md_alg;
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001837 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001838 if( status != PSA_SUCCESS )
1839 return( status );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001840
Gilles Peskine526fab02018-06-27 18:19:40 +02001841 if( signature_length < rsa->len )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001842 return( PSA_ERROR_BUFFER_TOO_SMALL );
1843#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02001844 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001845 {
1846 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1847 MBEDTLS_MD_NONE );
1848
1849 ret = mbedtls_rsa_pkcs1_verify( rsa,
Gilles Peskine61b91d42018-06-08 16:09:36 +02001850 mbedtls_ctr_drbg_random,
1851 &global_data.ctr_drbg,
1852 MBEDTLS_RSA_PUBLIC,
1853 md_alg,
1854 hash_length,
1855 hash,
1856 signature );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001857
1858 }
1859 else
1860#endif /* MBEDTLS_PKCS1_V15 */
1861#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02001862 if( PSA_ALG_IS_RSA_PSS( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001863 {
Gilles Peskinebeb49482018-06-08 17:44:35 +02001864 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1865 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
1866 mbedtls_ctr_drbg_random,
1867 &global_data.ctr_drbg,
1868 MBEDTLS_RSA_PUBLIC,
1869 md_alg, hash_length, hash,
1870 signature );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001871 }
1872 else
1873#endif /* MBEDTLS_PKCS1_V21 */
1874 {
1875 return( PSA_ERROR_INVALID_ARGUMENT );
1876 }
1877 return( mbedtls_to_psa_error( ret ) );
1878 }
1879 else
1880#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03001881#if defined(MBEDTLS_ECP_C)
1882 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1883 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001884#if defined(MBEDTLS_ECDSA_C)
1885 if( PSA_ALG_IS_ECDSA( alg ) )
1886 {
1887 int ret;
1888 ret = mbedtls_ecdsa_read_signature( slot->data.ecp,
1889 hash, hash_length,
1890 signature, signature_length );
1891 return( mbedtls_to_psa_error( ret ) );
1892 }
1893 else
1894#endif /* defined(MBEDTLS_ECDSA_C) */
1895 {
1896 return( PSA_ERROR_INVALID_ARGUMENT );
1897 }
itayzafrir5c753392018-05-08 11:18:38 +03001898 }
Gilles Peskine20035e32018-02-03 22:44:14 +01001899 else
1900#endif /* defined(MBEDTLS_ECP_C) */
1901 {
1902 return( PSA_ERROR_NOT_SUPPORTED );
1903 }
1904}
1905
Gilles Peskine61b91d42018-06-08 16:09:36 +02001906psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
1907 psa_algorithm_t alg,
1908 const uint8_t *input,
1909 size_t input_length,
1910 const uint8_t *salt,
1911 size_t salt_length,
1912 uint8_t *output,
1913 size_t output_size,
1914 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001915{
1916 key_slot_t *slot;
1917 (void) salt;
1918 (void) salt_length;
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03001919 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001920
Gilles Peskine828ed142018-06-18 23:25:51 +02001921 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Nir Sonnenschein7f5a3192018-05-06 22:26:54 +03001922 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001923 slot = &global_data.key_slots[key];
1924 if( slot->type == PSA_KEY_TYPE_NONE )
1925 return( PSA_ERROR_EMPTY_SLOT );
1926 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
1927 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001928 if( ! ( slot->policy.usage & PSA_KEY_USAGE_ENCRYPT ) )
1929 return( PSA_ERROR_NOT_PERMITTED );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001930
1931#if defined(MBEDTLS_RSA_C)
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001932 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ||
1933 slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001934 {
1935 mbedtls_rsa_context *rsa = slot->data.rsa;
1936 int ret;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001937 if( output_size < rsa->len )
Gilles Peskine61b91d42018-06-08 16:09:36 +02001938 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001939#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02001940 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001941 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001942 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
1943 mbedtls_ctr_drbg_random,
1944 &global_data.ctr_drbg,
1945 MBEDTLS_RSA_PUBLIC,
1946 input_length,
1947 input,
1948 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001949 }
1950 else
1951#endif /* MBEDTLS_PKCS1_V15 */
1952#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02001953 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001954 {
1955 return( PSA_ERROR_NOT_SUPPORTED );
1956 }
1957 else
1958#endif /* MBEDTLS_PKCS1_V21 */
1959 {
1960 return( PSA_ERROR_INVALID_ARGUMENT );
1961 }
1962 if( ret == 0 )
Nir Sonnenschein717a0402018-06-04 16:36:15 +03001963 *output_length = rsa->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001964 return( mbedtls_to_psa_error( ret ) );
1965 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001966 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02001967#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001968 {
1969 return( PSA_ERROR_NOT_SUPPORTED );
1970 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001971}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001972
Gilles Peskine61b91d42018-06-08 16:09:36 +02001973psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
1974 psa_algorithm_t alg,
1975 const uint8_t *input,
1976 size_t input_length,
1977 const uint8_t *salt,
1978 size_t salt_length,
1979 uint8_t *output,
1980 size_t output_size,
1981 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001982{
1983 key_slot_t *slot;
1984 (void) salt;
1985 (void) salt_length;
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03001986 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001987
Gilles Peskine828ed142018-06-18 23:25:51 +02001988 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001989 return( PSA_ERROR_EMPTY_SLOT );
1990 slot = &global_data.key_slots[key];
1991 if( slot->type == PSA_KEY_TYPE_NONE )
1992 return( PSA_ERROR_EMPTY_SLOT );
1993 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
1994 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001995 if( ! ( slot->policy.usage & PSA_KEY_USAGE_DECRYPT ) )
1996 return( PSA_ERROR_NOT_PERMITTED );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001997
1998#if defined(MBEDTLS_RSA_C)
1999 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2000 {
2001 mbedtls_rsa_context *rsa = slot->data.rsa;
2002 int ret;
2003
Gilles Peskinec4def2f2018-06-08 17:53:48 +02002004 if( input_length != rsa->len )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002005 return( PSA_ERROR_INVALID_ARGUMENT );
2006
2007#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002008 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002009 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002010 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2011 mbedtls_ctr_drbg_random,
2012 &global_data.ctr_drbg,
2013 MBEDTLS_RSA_PRIVATE,
2014 output_length,
2015 input,
2016 output,
2017 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002018 }
2019 else
2020#endif /* MBEDTLS_PKCS1_V15 */
2021#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002022 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002023 {
2024 return( PSA_ERROR_NOT_SUPPORTED );
2025 }
2026 else
2027#endif /* MBEDTLS_PKCS1_V21 */
2028 {
2029 return( PSA_ERROR_INVALID_ARGUMENT );
2030 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002031
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002032 return( mbedtls_to_psa_error( ret ) );
2033 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002034 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002035#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002036 {
2037 return( PSA_ERROR_NOT_SUPPORTED );
2038 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002039}
Gilles Peskine20035e32018-02-03 22:44:14 +01002040
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002041
2042
mohammad1603503973b2018-03-12 15:59:30 +02002043/****************************************************************/
2044/* Symmetric cryptography */
2045/****************************************************************/
2046
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002047/* Initialize the cipher operation structure. Once this function has been
2048 * called, psa_cipher_abort can run and will do the right thing. */
2049static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2050 psa_algorithm_t alg )
2051{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002052 if( ! PSA_ALG_IS_CIPHER( alg ) )
2053 {
2054 memset( operation, 0, sizeof( *operation ) );
2055 return( PSA_ERROR_INVALID_ARGUMENT );
2056 }
2057
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002058 operation->alg = alg;
2059 operation->key_set = 0;
2060 operation->iv_set = 0;
2061 operation->iv_required = 1;
2062 operation->iv_size = 0;
2063 operation->block_size = 0;
2064 mbedtls_cipher_init( &operation->ctx.cipher );
2065 return( PSA_SUCCESS );
2066}
2067
Gilles Peskinee553c652018-06-04 16:22:46 +02002068static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
2069 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02002070 psa_algorithm_t alg,
2071 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002072{
2073 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2074 psa_status_t status;
2075 key_slot_t *slot;
2076 psa_key_type_t key_type;
2077 size_t key_bits;
2078 const mbedtls_cipher_info_t *cipher_info = NULL;
2079
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002080 status = psa_cipher_init( operation, alg );
2081 if( status != PSA_SUCCESS )
2082 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002083
2084 status = psa_get_key_information( key, &key_type, &key_bits );
2085 if( status != PSA_SUCCESS )
2086 return( status );
2087 slot = &global_data.key_slots[key];
2088
Gilles Peskinebb1072f2018-06-08 18:46:05 +02002089 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002090 if( cipher_info == NULL )
2091 return( PSA_ERROR_NOT_SUPPORTED );
2092
mohammad1603503973b2018-03-12 15:59:30 +02002093 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002094 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002095 {
2096 psa_cipher_abort( operation );
2097 return( mbedtls_to_psa_error( ret ) );
2098 }
2099
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002100#if defined(MBEDTLS_DES_C)
2101 if( key_type == PSA_KEY_TYPE_DES && key_bits == 128 )
2102 {
2103 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2104 unsigned char keys[24];
2105 memcpy( keys, slot->data.raw.data, 16 );
2106 memcpy( keys + 16, slot->data.raw.data, 8 );
2107 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2108 keys,
2109 192, cipher_operation );
2110 }
2111 else
2112#endif
2113 {
2114 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2115 slot->data.raw.data,
2116 key_bits, cipher_operation );
2117 }
Moran Peker41deec42018-04-04 15:43:05 +03002118 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002119 {
2120 psa_cipher_abort( operation );
2121 return( mbedtls_to_psa_error( ret ) );
2122 }
2123
mohammad16038481e742018-03-18 13:57:31 +02002124#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Moran Peker7cb22b82018-06-05 11:40:02 +03002125 if( ( alg & ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK ) == PSA_ALG_CBC_BASE )
mohammad16038481e742018-03-18 13:57:31 +02002126 {
Gilles Peskine53514202018-06-06 15:11:46 +02002127 psa_algorithm_t padding_mode = alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
2128 mbedtls_cipher_padding_t mode;
mohammad16038481e742018-03-18 13:57:31 +02002129
Moran Pekera28258c2018-05-29 16:25:04 +03002130 switch ( padding_mode )
mohammad16038481e742018-03-18 13:57:31 +02002131 {
2132 case PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:
2133 mode = MBEDTLS_PADDING_PKCS7;
2134 break;
2135 case PSA_ALG_BLOCK_CIPHER_PAD_NONE:
2136 mode = MBEDTLS_PADDING_NONE;
2137 break;
2138 default:
Moran Pekerae382792018-05-31 14:06:17 +03002139 psa_cipher_abort( operation );
2140 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038481e742018-03-18 13:57:31 +02002141 }
2142 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, mode );
Moran Pekera28258c2018-05-29 16:25:04 +03002143 if( ret != 0 )
Moran Peker71f19ae2018-04-22 20:23:16 +03002144 {
2145 psa_cipher_abort( operation );
mohammad16038481e742018-03-18 13:57:31 +02002146 return( mbedtls_to_psa_error( ret ) );
Moran Peker71f19ae2018-04-22 20:23:16 +03002147 }
mohammad16038481e742018-03-18 13:57:31 +02002148 }
2149#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2150
mohammad1603503973b2018-03-12 15:59:30 +02002151 operation->key_set = 1;
Gilles Peskine7e928852018-06-04 16:23:10 +02002152 operation->block_size = ( PSA_ALG_IS_BLOCK_CIPHER( alg ) ?
2153 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) :
2154 1 );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002155 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) || alg == PSA_ALG_CTR )
mohammad16038481e742018-03-18 13:57:31 +02002156 {
mohammad160389e0f462018-04-12 08:48:45 +03002157 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
mohammad16038481e742018-03-18 13:57:31 +02002158 }
mohammad1603503973b2018-03-12 15:59:30 +02002159
Moran Peker395db872018-05-31 14:07:14 +03002160 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002161}
2162
Gilles Peskinee553c652018-06-04 16:22:46 +02002163psa_status_t psa_encrypt_setup( psa_cipher_operation_t *operation,
2164 psa_key_slot_t key,
2165 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002166{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002167 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002168}
2169
Gilles Peskinee553c652018-06-04 16:22:46 +02002170psa_status_t psa_decrypt_setup( psa_cipher_operation_t *operation,
2171 psa_key_slot_t key,
2172 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002173{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002174 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002175}
2176
Gilles Peskinee553c652018-06-04 16:22:46 +02002177psa_status_t psa_encrypt_generate_iv( psa_cipher_operation_t *operation,
2178 unsigned char *iv,
2179 size_t iv_size,
2180 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002181{
Moran Peker41deec42018-04-04 15:43:05 +03002182 int ret = PSA_SUCCESS;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002183 if( operation->iv_set || ! operation->iv_required )
Moran Peker41deec42018-04-04 15:43:05 +03002184 return( PSA_ERROR_BAD_STATE );
2185 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002186 {
Moran Peker41deec42018-04-04 15:43:05 +03002187 ret = PSA_ERROR_BUFFER_TOO_SMALL;
2188 goto exit;
2189 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002190 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2191 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002192 if( ret != 0 )
2193 {
2194 ret = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002195 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002196 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002197
mohammad16038481e742018-03-18 13:57:31 +02002198 *iv_length = operation->iv_size;
mohammad160389e0f462018-04-12 08:48:45 +03002199 ret = psa_encrypt_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002200
Moran Peker395db872018-05-31 14:07:14 +03002201exit:
2202 if( ret != PSA_SUCCESS )
2203 psa_cipher_abort( operation );
2204 return( ret );
mohammad1603503973b2018-03-12 15:59:30 +02002205}
2206
Gilles Peskinee553c652018-06-04 16:22:46 +02002207psa_status_t psa_encrypt_set_iv( psa_cipher_operation_t *operation,
2208 const unsigned char *iv,
2209 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002210{
Moran Peker41deec42018-04-04 15:43:05 +03002211 int ret = PSA_SUCCESS;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002212 if( operation->iv_set || ! operation->iv_required )
Moran Peker41deec42018-04-04 15:43:05 +03002213 return( PSA_ERROR_BAD_STATE );
Moran Pekera28258c2018-05-29 16:25:04 +03002214 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002215 {
Moran Pekerae382792018-05-31 14:06:17 +03002216 psa_cipher_abort( operation );
2217 return( PSA_ERROR_INVALID_ARGUMENT );
Moran Peker71f19ae2018-04-22 20:23:16 +03002218 }
mohammad1603503973b2018-03-12 15:59:30 +02002219 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002220 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002221 {
2222 psa_cipher_abort( operation );
2223 return( mbedtls_to_psa_error( ret ) );
2224 }
2225
2226 operation->iv_set = 1;
mohammad1603503973b2018-03-12 15:59:30 +02002227
Moran Peker395db872018-05-31 14:07:14 +03002228 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002229}
2230
Gilles Peskinee553c652018-06-04 16:22:46 +02002231psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2232 const uint8_t *input,
2233 size_t input_length,
2234 unsigned char *output,
2235 size_t output_size,
2236 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002237{
2238 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002239 size_t expected_output_size;
2240 if( PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
2241 {
2242 /* Take the unprocessed partial block left over from previous
2243 * update calls, if any, plus the input to this call. Remove
2244 * the last partial block, if any. You get the data that will be
2245 * output in this call. */
2246 expected_output_size =
2247 ( operation->ctx.cipher.unprocessed_len + input_length )
2248 / operation->block_size * operation->block_size;
2249 }
2250 else
2251 {
2252 expected_output_size = input_length;
2253 }
2254 if( output_size < expected_output_size )
Moran Peker395db872018-05-31 14:07:14 +03002255 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160382759612018-03-12 18:16:40 +02002256
mohammad1603503973b2018-03-12 15:59:30 +02002257 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002258 input_length, output, output_length );
Moran Peker41deec42018-04-04 15:43:05 +03002259 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002260 {
2261 psa_cipher_abort( operation );
2262 return( mbedtls_to_psa_error( ret ) );
2263 }
2264
Moran Peker395db872018-05-31 14:07:14 +03002265 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002266}
2267
Gilles Peskinee553c652018-06-04 16:22:46 +02002268psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2269 uint8_t *output,
2270 size_t output_size,
2271 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002272{
2273 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002274 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002275
mohammad1603503973b2018-03-12 15:59:30 +02002276 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002277 {
Moran Peker7cb22b82018-06-05 11:40:02 +03002278 psa_cipher_abort( operation );
2279 return( PSA_ERROR_BAD_STATE );
2280 }
2281 if( operation->iv_required && ! operation->iv_set )
2282 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002283 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03002284 return( PSA_ERROR_BAD_STATE );
2285 }
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002286 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
2287 PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
Moran Peker7cb22b82018-06-05 11:40:02 +03002288 {
Gilles Peskine53514202018-06-06 15:11:46 +02002289 psa_algorithm_t padding_mode =
2290 operation->alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
Moran Peker7cb22b82018-06-05 11:40:02 +03002291 if( operation->ctx.cipher.unprocessed_len >= operation->block_size )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002292 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002293 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03002294 return( PSA_ERROR_TAMPERING_DETECTED );
2295 }
Gilles Peskine53514202018-06-06 15:11:46 +02002296 if( padding_mode == PSA_ALG_BLOCK_CIPHER_PAD_NONE )
Moran Peker7cb22b82018-06-05 11:40:02 +03002297 {
2298 if( operation->ctx.cipher.unprocessed_len != 0 )
2299 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002300 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03002301 return( PSA_ERROR_INVALID_ARGUMENT );
2302 }
Gilles Peskine89d789c2018-06-04 17:17:16 +02002303 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002304 }
2305
2306 ret = mbedtls_cipher_finish( &operation->ctx.cipher, temp_output_buffer,
Gilles Peskinee553c652018-06-04 16:22:46 +02002307 output_length );
Moran Peker41deec42018-04-04 15:43:05 +03002308 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002309 {
2310 psa_cipher_abort( operation );
2311 return( mbedtls_to_psa_error( ret ) );
2312 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002313 if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002314 memcpy( output, temp_output_buffer, *output_length );
2315 else
2316 {
2317 psa_cipher_abort( operation );
2318 return( PSA_ERROR_BUFFER_TOO_SMALL );
2319 }
mohammad1603503973b2018-03-12 15:59:30 +02002320
Moran Peker4c80d832018-04-22 20:15:31 +03002321 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002322}
2323
Gilles Peskinee553c652018-06-04 16:22:46 +02002324psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2325{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002326 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02002327 {
2328 /* The object has (apparently) been initialized but it is not
2329 * in use. It's ok to call abort on such an object, and there's
2330 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002331 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02002332 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002333
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002334 /* Sanity check (shouldn't happen: operation->alg should
2335 * always have been initialized to a valid value). */
2336 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
2337 return( PSA_ERROR_BAD_STATE );
2338
mohammad1603503973b2018-03-12 15:59:30 +02002339 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002340
Moran Peker41deec42018-04-04 15:43:05 +03002341 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002342 operation->key_set = 0;
2343 operation->iv_set = 0;
2344 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002345 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002346 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002347
Moran Peker395db872018-05-31 14:07:14 +03002348 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002349}
2350
Gilles Peskinea0655c32018-04-30 17:06:50 +02002351
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002352
mohammad16038cc1cee2018-03-28 01:21:33 +03002353/****************************************************************/
2354/* Key Policy */
2355/****************************************************************/
2356
Gilles Peskine2d277862018-06-18 15:41:12 +02002357void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002358{
Gilles Peskine803ce742018-06-18 16:07:14 +02002359 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03002360}
2361
Gilles Peskine2d277862018-06-18 15:41:12 +02002362void psa_key_policy_set_usage( psa_key_policy_t *policy,
2363 psa_key_usage_t usage,
2364 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03002365{
mohammad16034eed7572018-03-28 05:14:59 -07002366 policy->usage = usage;
2367 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03002368}
2369
Gilles Peskine2d277862018-06-18 15:41:12 +02002370psa_key_usage_t psa_key_policy_get_usage( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002371{
mohammad16036df908f2018-04-02 08:34:15 -07002372 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03002373}
2374
Gilles Peskine2d277862018-06-18 15:41:12 +02002375psa_algorithm_t psa_key_policy_get_algorithm( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002376{
mohammad16036df908f2018-04-02 08:34:15 -07002377 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03002378}
2379
Gilles Peskine2d277862018-06-18 15:41:12 +02002380psa_status_t psa_set_key_policy( psa_key_slot_t key,
2381 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002382{
2383 key_slot_t *slot;
mohammad16038cc1cee2018-03-28 01:21:33 +03002384
Gilles Peskine828ed142018-06-18 23:25:51 +02002385 if( key == 0 || key > PSA_KEY_SLOT_COUNT || policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002386 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002387
mohammad16038cc1cee2018-03-28 01:21:33 +03002388 slot = &global_data.key_slots[key];
2389 if( slot->type != PSA_KEY_TYPE_NONE )
2390 return( PSA_ERROR_OCCUPIED_SLOT );
2391
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002392 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
2393 PSA_KEY_USAGE_ENCRYPT |
2394 PSA_KEY_USAGE_DECRYPT |
2395 PSA_KEY_USAGE_SIGN |
2396 PSA_KEY_USAGE_VERIFY ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07002397 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03002398
mohammad16036df908f2018-04-02 08:34:15 -07002399 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002400
2401 return( PSA_SUCCESS );
2402}
2403
Gilles Peskine2d277862018-06-18 15:41:12 +02002404psa_status_t psa_get_key_policy( psa_key_slot_t key,
2405 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002406{
2407 key_slot_t *slot;
2408
Gilles Peskine828ed142018-06-18 23:25:51 +02002409 if( key == 0 || key > PSA_KEY_SLOT_COUNT || policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002410 return( PSA_ERROR_INVALID_ARGUMENT );
2411
2412 slot = &global_data.key_slots[key];
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002413
mohammad16036df908f2018-04-02 08:34:15 -07002414 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002415
2416 return( PSA_SUCCESS );
2417}
Gilles Peskine20035e32018-02-03 22:44:14 +01002418
Gilles Peskinea0655c32018-04-30 17:06:50 +02002419
2420
mohammad1603804cd712018-03-20 22:44:08 +02002421/****************************************************************/
2422/* Key Lifetime */
2423/****************************************************************/
2424
Gilles Peskine2d277862018-06-18 15:41:12 +02002425psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
2426 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002427{
2428 key_slot_t *slot;
2429
Gilles Peskine828ed142018-06-18 23:25:51 +02002430 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
mohammad1603804cd712018-03-20 22:44:08 +02002431 return( PSA_ERROR_INVALID_ARGUMENT );
2432
2433 slot = &global_data.key_slots[key];
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002434
mohammad1603804cd712018-03-20 22:44:08 +02002435 *lifetime = slot->lifetime;
2436
2437 return( PSA_SUCCESS );
2438}
2439
Gilles Peskine2d277862018-06-18 15:41:12 +02002440psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
2441 const psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002442{
2443 key_slot_t *slot;
2444
Gilles Peskine828ed142018-06-18 23:25:51 +02002445 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
mohammad1603804cd712018-03-20 22:44:08 +02002446 return( PSA_ERROR_INVALID_ARGUMENT );
2447
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002448 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
2449 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
mohammad1603ba178512018-03-21 04:35:20 -07002450 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE)
2451 return( PSA_ERROR_INVALID_ARGUMENT );
2452
mohammad1603804cd712018-03-20 22:44:08 +02002453 slot = &global_data.key_slots[key];
mohammad16035d7ec202018-03-28 01:29:41 +03002454 if( slot->type != PSA_KEY_TYPE_NONE )
2455 return( PSA_ERROR_OCCUPIED_SLOT );
mohammad1603804cd712018-03-20 22:44:08 +02002456
Moran Pekerd7326592018-05-29 16:56:39 +03002457 if( lifetime != PSA_KEY_LIFETIME_VOLATILE )
mohammad1603ba178512018-03-21 04:35:20 -07002458 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002459
mohammad1603060ad8a2018-03-20 14:28:38 -07002460 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02002461
2462 return( PSA_SUCCESS );
2463}
2464
Gilles Peskine20035e32018-02-03 22:44:14 +01002465
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002466
mohammad16035955c982018-04-26 00:53:03 +03002467/****************************************************************/
2468/* AEAD */
2469/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002470
mohammad16035955c982018-04-26 00:53:03 +03002471psa_status_t psa_aead_encrypt( psa_key_slot_t key,
2472 psa_algorithm_t alg,
2473 const uint8_t *nonce,
2474 size_t nonce_length,
2475 const uint8_t *additional_data,
2476 size_t additional_data_length,
2477 const uint8_t *plaintext,
2478 size_t plaintext_length,
2479 uint8_t *ciphertext,
2480 size_t ciphertext_size,
2481 size_t *ciphertext_length )
2482{
2483 int ret;
2484 psa_status_t status;
2485 key_slot_t *slot;
2486 psa_key_type_t key_type;
2487 size_t key_bits;
mohammad160315223a82018-06-03 17:19:55 +03002488 uint8_t *tag;
2489 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002490 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002491 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002492
mohammad1603f08a5502018-06-03 15:05:47 +03002493 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07002494
mohammad16035955c982018-04-26 00:53:03 +03002495 status = psa_get_key_information( key, &key_type, &key_bits );
2496 if( status != PSA_SUCCESS )
2497 return( status );
2498 slot = &global_data.key_slots[key];
mohammad1603a1d98012018-06-06 13:45:55 +03002499 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskine2d277862018-06-18 15:41:12 +02002500 return( PSA_ERROR_EMPTY_SLOT );
mohammad16035955c982018-04-26 00:53:03 +03002501
mohammad16035ed06212018-06-06 13:09:34 +03002502 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type,
2503 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002504 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002505 return( PSA_ERROR_NOT_SUPPORTED );
mohammad1603dad36fa2018-05-09 02:24:42 -07002506
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002507 if( ( slot->policy.usage & PSA_KEY_USAGE_ENCRYPT ) == 0 )
mohammad1603f14394b2018-06-04 14:33:19 +03002508 return( PSA_ERROR_NOT_PERMITTED );
mohammad16035955c982018-04-26 00:53:03 +03002509
Gilles Peskine2d277862018-06-18 15:41:12 +02002510 if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
2511 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603db624732018-04-30 17:21:50 +03002512 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002513
mohammad16035955c982018-04-26 00:53:03 +03002514 if( alg == PSA_ALG_GCM )
2515 {
2516 mbedtls_gcm_context gcm;
mohammad160315223a82018-06-03 17:19:55 +03002517 tag_length = 16;
2518
mohammad160396910d82018-06-04 14:33:00 +03002519 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) != 16 )
2520 return( PSA_ERROR_INVALID_ARGUMENT );
2521
mohammad160315223a82018-06-03 17:19:55 +03002522 //make sure we have place to hold the tag in the ciphertext buffer
2523 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002524 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002525
2526 //update the tag pointer to point to the end of the ciphertext_length
2527 tag = ciphertext + plaintext_length;
2528
mohammad16035955c982018-04-26 00:53:03 +03002529 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002530 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
mohammad160395893f82018-06-03 15:06:17 +03002531 slot->data.raw.data,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002532 key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002533 if( ret != 0 )
2534 {
2535 mbedtls_gcm_free( &gcm );
2536 return( mbedtls_to_psa_error( ret ) );
2537 }
2538 ret = mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002539 plaintext_length, nonce,
2540 nonce_length, additional_data,
2541 additional_data_length, plaintext,
mohammad160315223a82018-06-03 17:19:55 +03002542 ciphertext, tag_length, tag );
mohammad16035955c982018-04-26 00:53:03 +03002543 mbedtls_gcm_free( &gcm );
2544 }
2545 else if( alg == PSA_ALG_CCM )
2546 {
2547 mbedtls_ccm_context ccm;
mohammad160315223a82018-06-03 17:19:55 +03002548 tag_length = 16;
2549
mohammad160396910d82018-06-04 14:33:00 +03002550 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) != 16 )
2551 return( PSA_ERROR_INVALID_ARGUMENT );
2552
mohammad160347ddf3d2018-04-26 01:11:21 +03002553 if( nonce_length < 7 || nonce_length > 13 )
2554 return( PSA_ERROR_INVALID_ARGUMENT );
2555
mohammad160315223a82018-06-03 17:19:55 +03002556 //make sure we have place to hold the tag in the ciphertext buffer
2557 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002558 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002559
2560 //update the tag pointer to point to the end of the ciphertext_length
2561 tag = ciphertext + plaintext_length;
2562
mohammad16035955c982018-04-26 00:53:03 +03002563 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002564 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
mohammad16036bbd8c72018-04-30 17:22:52 +03002565 slot->data.raw.data, key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002566 if( ret != 0 )
2567 {
2568 mbedtls_ccm_free( &ccm );
2569 return( mbedtls_to_psa_error( ret ) );
2570 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002571 ret = mbedtls_ccm_encrypt_and_tag( &ccm, plaintext_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002572 nonce, nonce_length,
2573 additional_data,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002574 additional_data_length,
2575 plaintext, ciphertext,
mohammad160315223a82018-06-03 17:19:55 +03002576 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002577 mbedtls_ccm_free( &ccm );
2578 }
mohammad16035c8845f2018-05-09 05:40:09 -07002579 else
2580 {
mohammad1603554faad2018-06-03 15:07:38 +03002581 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07002582 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002583
mohammad160315223a82018-06-03 17:19:55 +03002584 if( ret != 0 )
2585 {
2586 memset( ciphertext, 0, ciphertext_size );
2587 return( mbedtls_to_psa_error( ret ) );
2588 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002589
mohammad160315223a82018-06-03 17:19:55 +03002590 *ciphertext_length = plaintext_length + tag_length;
mohammad160347ddf3d2018-04-26 01:11:21 +03002591 return( PSA_SUCCESS );
mohammad16035955c982018-04-26 00:53:03 +03002592}
2593
Gilles Peskineee652a32018-06-01 19:23:52 +02002594/* Locate the tag in a ciphertext buffer containing the encrypted data
2595 * followed by the tag. Return the length of the part preceding the tag in
2596 * *plaintext_length. This is the size of the plaintext in modes where
2597 * the encrypted data has the same size as the plaintext, such as
2598 * CCM and GCM. */
2599static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
2600 const uint8_t *ciphertext,
2601 size_t ciphertext_length,
2602 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02002603 const uint8_t **p_tag )
2604{
2605 size_t payload_length;
2606 if( tag_length > ciphertext_length )
2607 return( PSA_ERROR_INVALID_ARGUMENT );
2608 payload_length = ciphertext_length - tag_length;
2609 if( payload_length > plaintext_size )
2610 return( PSA_ERROR_BUFFER_TOO_SMALL );
2611 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02002612 return( PSA_SUCCESS );
2613}
2614
mohammad16035955c982018-04-26 00:53:03 +03002615psa_status_t psa_aead_decrypt( psa_key_slot_t key,
2616 psa_algorithm_t alg,
2617 const uint8_t *nonce,
2618 size_t nonce_length,
2619 const uint8_t *additional_data,
2620 size_t additional_data_length,
2621 const uint8_t *ciphertext,
2622 size_t ciphertext_length,
2623 uint8_t *plaintext,
2624 size_t plaintext_size,
2625 size_t *plaintext_length )
2626{
2627 int ret;
2628 psa_status_t status;
2629 key_slot_t *slot;
2630 psa_key_type_t key_type;
2631 size_t key_bits;
Gilles Peskineee652a32018-06-01 19:23:52 +02002632 const uint8_t *tag;
2633 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002634 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002635 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002636
Gilles Peskineee652a32018-06-01 19:23:52 +02002637 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03002638
mohammad16035955c982018-04-26 00:53:03 +03002639 status = psa_get_key_information( key, &key_type, &key_bits );
2640 if( status != PSA_SUCCESS )
2641 return( status );
2642 slot = &global_data.key_slots[key];
mohammad1603a1d98012018-06-06 13:45:55 +03002643 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskine2d277862018-06-18 15:41:12 +02002644 return( PSA_ERROR_EMPTY_SLOT );
mohammad16035955c982018-04-26 00:53:03 +03002645
mohammad16035ed06212018-06-06 13:09:34 +03002646 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type,
2647 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002648 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002649 return( PSA_ERROR_NOT_SUPPORTED );
2650
mohammad1603f14394b2018-06-04 14:33:19 +03002651 if( !( slot->policy.usage & PSA_KEY_USAGE_DECRYPT ) )
2652 return( PSA_ERROR_NOT_PERMITTED );
mohammad16035955c982018-04-26 00:53:03 +03002653
Gilles Peskine2d277862018-06-18 15:41:12 +02002654 if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
2655 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603a7e6df72018-04-30 17:25:45 +03002656 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002657
mohammad16035955c982018-04-26 00:53:03 +03002658 if( alg == PSA_ALG_GCM )
2659 {
2660 mbedtls_gcm_context gcm;
mohammad160347ddf3d2018-04-26 01:11:21 +03002661
Gilles Peskineee652a32018-06-01 19:23:52 +02002662 tag_length = 16;
2663 status = psa_aead_unpadded_locate_tag( tag_length,
2664 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002665 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02002666 if( status != PSA_SUCCESS )
2667 return( status );
2668
mohammad16035955c982018-04-26 00:53:03 +03002669 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002670 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
mohammad16036bbd8c72018-04-30 17:22:52 +03002671 slot->data.raw.data, key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002672 if( ret != 0 )
2673 {
2674 mbedtls_gcm_free( &gcm );
2675 return( mbedtls_to_psa_error( ret ) );
2676 }
mohammad16035955c982018-04-26 00:53:03 +03002677
Gilles Peskineee652a32018-06-01 19:23:52 +02002678 ret = mbedtls_gcm_auth_decrypt( &gcm,
mohammad160360a64d02018-06-03 17:20:42 +03002679 ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002680 nonce, nonce_length,
mohammad16035ed06212018-06-06 13:09:34 +03002681 additional_data,
2682 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002683 tag, tag_length,
2684 ciphertext, plaintext );
mohammad16035955c982018-04-26 00:53:03 +03002685 mbedtls_gcm_free( &gcm );
2686 }
2687 else if( alg == PSA_ALG_CCM )
2688 {
2689 mbedtls_ccm_context ccm;
Gilles Peskinea40d7742018-06-01 16:28:30 +02002690
mohammad160347ddf3d2018-04-26 01:11:21 +03002691 if( nonce_length < 7 || nonce_length > 13 )
2692 return( PSA_ERROR_INVALID_ARGUMENT );
2693
mohammad16039375f842018-06-03 14:28:24 +03002694 tag_length = 16;
2695 status = psa_aead_unpadded_locate_tag( tag_length,
2696 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002697 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03002698 if( status != PSA_SUCCESS )
2699 return( status );
2700
mohammad16035955c982018-04-26 00:53:03 +03002701 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002702 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
mohammad16036bbd8c72018-04-30 17:22:52 +03002703 slot->data.raw.data, key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002704 if( ret != 0 )
2705 {
2706 mbedtls_ccm_free( &ccm );
2707 return( mbedtls_to_psa_error( ret ) );
2708 }
mohammad160360a64d02018-06-03 17:20:42 +03002709 ret = mbedtls_ccm_auth_decrypt( &ccm, ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002710 nonce, nonce_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002711 additional_data,
2712 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002713 ciphertext, plaintext,
2714 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002715 mbedtls_ccm_free( &ccm );
2716 }
mohammad160339574652018-06-01 04:39:53 -07002717 else
2718 {
mohammad1603554faad2018-06-03 15:07:38 +03002719 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07002720 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002721
Gilles Peskineee652a32018-06-01 19:23:52 +02002722 if( ret != 0 )
mohammad160360a64d02018-06-03 17:20:42 +03002723 memset( plaintext, 0, plaintext_size );
2724 else
2725 *plaintext_length = ciphertext_length - tag_length;
2726
Gilles Peskineee652a32018-06-01 19:23:52 +02002727 return( mbedtls_to_psa_error( ret ) );
mohammad16035955c982018-04-26 00:53:03 +03002728}
2729
Gilles Peskinea0655c32018-04-30 17:06:50 +02002730
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002731
Gilles Peskine20035e32018-02-03 22:44:14 +01002732/****************************************************************/
Gilles Peskine05d69892018-06-19 22:00:52 +02002733/* Key generation */
2734/****************************************************************/
2735
2736psa_status_t psa_generate_random( uint8_t *output,
2737 size_t output_size )
2738{
2739 int ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2740 output, output_size );
2741 return( mbedtls_to_psa_error( ret ) );
2742}
2743
2744psa_status_t psa_generate_key( psa_key_slot_t key,
2745 psa_key_type_t type,
2746 size_t bits,
2747 const void *parameters,
2748 size_t parameters_size )
2749{
Gilles Peskine12313cd2018-06-20 00:20:32 +02002750 key_slot_t *slot;
2751
2752 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
2753 return( PSA_ERROR_INVALID_ARGUMENT );
2754 slot = &global_data.key_slots[key];
2755 if( slot->type != PSA_KEY_TYPE_NONE )
2756 return( PSA_ERROR_OCCUPIED_SLOT );
2757 if( parameters == NULL && parameters_size != 0 )
2758 return( PSA_ERROR_INVALID_ARGUMENT );
2759
Gilles Peskine48c0ea12018-06-21 14:15:31 +02002760 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02002761 {
2762 psa_status_t status = prepare_raw_data_slot( type, bits,
2763 &slot->data.raw );
2764 if( status != PSA_SUCCESS )
2765 return( status );
2766 status = psa_generate_random( slot->data.raw.data,
2767 slot->data.raw.bytes );
2768 if( status != PSA_SUCCESS )
2769 {
2770 mbedtls_free( slot->data.raw.data );
2771 return( status );
2772 }
2773#if defined(MBEDTLS_DES_C)
2774 if( type == PSA_KEY_TYPE_DES )
2775 {
2776 mbedtls_des_key_set_parity( slot->data.raw.data );
2777 if( slot->data.raw.bytes >= 16 )
2778 mbedtls_des_key_set_parity( slot->data.raw.data + 8 );
2779 if( slot->data.raw.bytes == 24 )
2780 mbedtls_des_key_set_parity( slot->data.raw.data + 16 );
2781 }
2782#endif /* MBEDTLS_DES_C */
2783 }
2784 else
2785
2786#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
2787 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
2788 {
2789 mbedtls_rsa_context *rsa;
2790 int ret;
2791 int exponent = 65537;
2792 if( parameters != NULL )
2793 {
2794 const unsigned *p = parameters;
2795 if( parameters_size != sizeof( *p ) )
2796 return( PSA_ERROR_INVALID_ARGUMENT );
2797 if( *p > INT_MAX )
2798 return( PSA_ERROR_INVALID_ARGUMENT );
2799 exponent = *p;
2800 }
2801 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
2802 if( rsa == NULL )
2803 return( PSA_ERROR_INSUFFICIENT_MEMORY );
2804 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
2805 ret = mbedtls_rsa_gen_key( rsa,
2806 mbedtls_ctr_drbg_random,
2807 &global_data.ctr_drbg,
2808 bits,
2809 exponent );
2810 if( ret != 0 )
2811 {
2812 mbedtls_rsa_free( rsa );
2813 mbedtls_free( rsa );
2814 return( mbedtls_to_psa_error( ret ) );
2815 }
2816 slot->data.rsa = rsa;
2817 }
2818 else
2819#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
2820
2821#if defined(MBEDTLS_ECP_C)
2822 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
2823 {
2824 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
2825 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
2826 const mbedtls_ecp_curve_info *curve_info =
2827 mbedtls_ecp_curve_info_from_grp_id( grp_id );
2828 mbedtls_ecp_keypair *ecp;
2829 int ret;
2830 if( parameters != NULL )
2831 return( PSA_ERROR_NOT_SUPPORTED );
2832 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
2833 return( PSA_ERROR_NOT_SUPPORTED );
2834 if( curve_info->bit_size != bits )
2835 return( PSA_ERROR_INVALID_ARGUMENT );
2836 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
2837 if( ecp == NULL )
2838 return( PSA_ERROR_INSUFFICIENT_MEMORY );
2839 mbedtls_ecp_keypair_init( ecp );
2840 ret = mbedtls_ecp_gen_key( grp_id, ecp,
2841 mbedtls_ctr_drbg_random,
2842 &global_data.ctr_drbg );
2843 if( ret != 0 )
2844 {
2845 mbedtls_ecp_keypair_free( ecp );
2846 mbedtls_free( ecp );
2847 return( mbedtls_to_psa_error( ret ) );
2848 }
2849 slot->data.ecp = ecp;
2850 }
2851 else
2852#endif /* MBEDTLS_ECP_C */
2853
2854 return( PSA_ERROR_NOT_SUPPORTED );
2855
2856 slot->type = type;
2857 return( PSA_SUCCESS );
Gilles Peskine05d69892018-06-19 22:00:52 +02002858}
2859
2860
2861/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002862/* Module setup */
2863/****************************************************************/
2864
Gilles Peskinee59236f2018-01-27 23:32:46 +01002865void mbedtls_psa_crypto_free( void )
2866{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002867 size_t key;
Gilles Peskine828ed142018-06-18 23:25:51 +02002868 for( key = 1; key < PSA_KEY_SLOT_COUNT; key++ )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002869 psa_destroy_key( key );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002870 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
2871 mbedtls_entropy_free( &global_data.entropy );
2872 mbedtls_zeroize( &global_data, sizeof( global_data ) );
2873}
2874
2875psa_status_t psa_crypto_init( void )
2876{
2877 int ret;
2878 const unsigned char drbg_seed[] = "PSA";
2879
2880 if( global_data.initialized != 0 )
2881 return( PSA_SUCCESS );
2882
2883 mbedtls_zeroize( &global_data, sizeof( global_data ) );
2884 mbedtls_entropy_init( &global_data.entropy );
2885 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
2886
2887 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
2888 mbedtls_entropy_func,
2889 &global_data.entropy,
2890 drbg_seed, sizeof( drbg_seed ) - 1 );
2891 if( ret != 0 )
2892 goto exit;
2893
Gilles Peskinee4ebc122018-03-07 14:16:44 +01002894 global_data.initialized = 1;
2895
Gilles Peskinee59236f2018-01-27 23:32:46 +01002896exit:
2897 if( ret != 0 )
2898 mbedtls_psa_crypto_free( );
2899 return( mbedtls_to_psa_error( ret ) );
2900}
2901
2902#endif /* MBEDTLS_PSA_CRYPTO_C */