blob: f6f2e58054c065257fe5cc77b0ac562edfc428d4 [file] [log] [blame]
Hanno Becker87837b22018-11-08 13:32:02 +00001/**
Hanno Beckerafebf5a2018-11-13 21:01:41 +00002 * \file psa_util.h
Hanno Becker87837b22018-11-08 13:32:02 +00003 *
4 * \brief Utility functions for the use of the PSA Crypto library.
5 *
6 * \warning This function is not part of the public API and may
7 * change at any time.
8 */
9/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020010 * Copyright The Mbed TLS Contributors
Hanno Becker87837b22018-11-08 13:32:02 +000011 * SPDX-License-Identifier: Apache-2.0
12 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Hanno Becker87837b22018-11-08 13:32:02 +000024 */
25
Hanno Becker186b65a2018-11-19 15:14:21 +000026#ifndef MBEDTLS_PSA_UTIL_H
27#define MBEDTLS_PSA_UTIL_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020028#include "mbedtls/private_access.h"
Hanno Becker87837b22018-11-08 13:32:02 +000029
Bence Szépkútic662b362021-05-27 11:25:03 +020030#include "mbedtls/build_info.h"
Hanno Becker87837b22018-11-08 13:32:02 +000031
32#if defined(MBEDTLS_USE_PSA_CRYPTO)
33
34#include "psa/crypto.h"
35
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010036#include "mbedtls/ecp.h"
37#include "mbedtls/md.h"
38#include "mbedtls/pk.h"
39#include "mbedtls/oid.h"
TRodziewicz3408d602021-04-21 13:25:25 +020040#include "mbedtls/error.h"
Hanno Becker87837b22018-11-08 13:32:02 +000041
Hanno Beckerf75f9122019-01-07 15:36:51 +000042#include <string.h>
43
Hanno Becker55251262018-11-12 09:29:12 +000044/* Translations for symmetric crypto. */
45
Hanno Beckerb26c1932018-11-12 10:18:57 +000046static inline psa_key_type_t mbedtls_psa_translate_cipher_type(
Hanno Becker55251262018-11-12 09:29:12 +000047 mbedtls_cipher_type_t cipher )
48{
49 switch( cipher )
50 {
51 case MBEDTLS_CIPHER_AES_128_CCM:
52 case MBEDTLS_CIPHER_AES_192_CCM:
53 case MBEDTLS_CIPHER_AES_256_CCM:
54 case MBEDTLS_CIPHER_AES_128_GCM:
55 case MBEDTLS_CIPHER_AES_192_GCM:
56 case MBEDTLS_CIPHER_AES_256_GCM:
57 case MBEDTLS_CIPHER_AES_128_CBC:
58 case MBEDTLS_CIPHER_AES_192_CBC:
59 case MBEDTLS_CIPHER_AES_256_CBC:
60 return( PSA_KEY_TYPE_AES );
61
62 /* ARIA not yet supported in PSA. */
63 /* case MBEDTLS_CIPHER_ARIA_128_CCM:
64 case MBEDTLS_CIPHER_ARIA_192_CCM:
65 case MBEDTLS_CIPHER_ARIA_256_CCM:
66 case MBEDTLS_CIPHER_ARIA_128_GCM:
67 case MBEDTLS_CIPHER_ARIA_192_GCM:
68 case MBEDTLS_CIPHER_ARIA_256_GCM:
69 case MBEDTLS_CIPHER_ARIA_128_CBC:
70 case MBEDTLS_CIPHER_ARIA_192_CBC:
71 case MBEDTLS_CIPHER_ARIA_256_CBC:
72 return( PSA_KEY_TYPE_ARIA ); */
73
74 default:
75 return( 0 );
76 }
77}
78
Hanno Beckerb26c1932018-11-12 10:18:57 +000079static inline psa_algorithm_t mbedtls_psa_translate_cipher_mode(
Hanno Becker010cf7e2018-11-15 15:48:57 +000080 mbedtls_cipher_mode_t mode, size_t taglen )
Hanno Becker55251262018-11-12 09:29:12 +000081{
82 switch( mode )
83 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +020084 case MBEDTLS_MODE_ECB:
Steven Cooremana6033e92020-08-25 11:47:50 +020085 return( PSA_ALG_ECB_NO_PADDING );
Hanno Becker55251262018-11-12 09:29:12 +000086 case MBEDTLS_MODE_GCM:
Bence Szépkútia63b20d2020-12-16 11:36:46 +010087 return( PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, taglen ) );
Hanno Becker55251262018-11-12 09:29:12 +000088 case MBEDTLS_MODE_CCM:
Bence Szépkútia63b20d2020-12-16 11:36:46 +010089 return( PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) );
Hanno Becker55251262018-11-12 09:29:12 +000090 case MBEDTLS_MODE_CBC:
Hanno Becker010cf7e2018-11-15 15:48:57 +000091 if( taglen == 0 )
92 return( PSA_ALG_CBC_NO_PADDING );
Paul Elliott36f539d2021-03-09 16:51:02 +000093 else
94 return( 0 );
Hanno Becker55251262018-11-12 09:29:12 +000095 default:
96 return( 0 );
97 }
98}
99
Hanno Beckerb26c1932018-11-12 10:18:57 +0000100static inline psa_key_usage_t mbedtls_psa_translate_cipher_operation(
101 mbedtls_operation_t op )
Hanno Becker55251262018-11-12 09:29:12 +0000102{
103 switch( op )
104 {
105 case MBEDTLS_ENCRYPT:
106 return( PSA_KEY_USAGE_ENCRYPT );
107 case MBEDTLS_DECRYPT:
108 return( PSA_KEY_USAGE_DECRYPT );
109 default:
110 return( 0 );
111 }
112}
113
114/* Translations for hashing. */
115
Hanno Beckerb26c1932018-11-12 10:18:57 +0000116static inline psa_algorithm_t mbedtls_psa_translate_md( mbedtls_md_type_t md_alg )
Hanno Becker87837b22018-11-08 13:32:02 +0000117{
118 switch( md_alg )
119 {
Hanno Becker87837b22018-11-08 13:32:02 +0000120#if defined(MBEDTLS_MD5_C)
121 case MBEDTLS_MD_MD5:
122 return( PSA_ALG_MD5 );
123#endif
124#if defined(MBEDTLS_SHA1_C)
125 case MBEDTLS_MD_SHA1:
126 return( PSA_ALG_SHA_1 );
127#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200128#if defined(MBEDTLS_SHA224_C)
Hanno Becker87837b22018-11-08 13:32:02 +0000129 case MBEDTLS_MD_SHA224:
130 return( PSA_ALG_SHA_224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200131#endif
132#if defined(MBEDTLS_SHA256_C)
Hanno Becker87837b22018-11-08 13:32:02 +0000133 case MBEDTLS_MD_SHA256:
134 return( PSA_ALG_SHA_256 );
135#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200136#if defined(MBEDTLS_SHA384_C)
Hanno Becker87837b22018-11-08 13:32:02 +0000137 case MBEDTLS_MD_SHA384:
138 return( PSA_ALG_SHA_384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200139#endif
140#if defined(MBEDTLS_SHA512_C)
Hanno Becker87837b22018-11-08 13:32:02 +0000141 case MBEDTLS_MD_SHA512:
142 return( PSA_ALG_SHA_512 );
143#endif
144#if defined(MBEDTLS_RIPEMD160_C)
145 case MBEDTLS_MD_RIPEMD160:
146 return( PSA_ALG_RIPEMD160 );
147#endif
Paul Elliott36f539d2021-03-09 16:51:02 +0000148 case MBEDTLS_MD_NONE:
149 return( 0 );
Hanno Becker87837b22018-11-08 13:32:02 +0000150 default:
151 return( 0 );
152 }
153}
154
Hanno Becker55251262018-11-12 09:29:12 +0000155/* Translations for ECC. */
156
Hanno Becker812e1242019-02-01 10:06:51 +0000157static inline int mbedtls_psa_get_ecc_oid_from_id(
Paul Elliott8ff510a2020-06-02 17:19:28 +0100158 psa_ecc_family_t curve, size_t bits,
Gilles Peskine89177e82019-12-03 21:19:09 +0100159 char const **oid, size_t *oid_len )
Hanno Becker812e1242019-02-01 10:06:51 +0000160{
161 switch( curve )
162 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100163 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine89177e82019-12-03 21:19:09 +0100164 switch( bits )
165 {
Hanno Becker812e1242019-02-01 10:06:51 +0000166#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100167 case 192:
168 *oid = MBEDTLS_OID_EC_GRP_SECP192R1;
169 *oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP192R1 );
170 return( 0 );
Hanno Becker812e1242019-02-01 10:06:51 +0000171#endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */
172#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100173 case 224:
174 *oid = MBEDTLS_OID_EC_GRP_SECP224R1;
175 *oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP224R1 );
176 return( 0 );
Hanno Becker812e1242019-02-01 10:06:51 +0000177#endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */
178#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100179 case 256:
180 *oid = MBEDTLS_OID_EC_GRP_SECP256R1;
181 *oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP256R1 );
182 return( 0 );
Hanno Becker812e1242019-02-01 10:06:51 +0000183#endif /* MBEDTLS_ECP_DP_SECP256R1_ENABLED */
184#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100185 case 384:
186 *oid = MBEDTLS_OID_EC_GRP_SECP384R1;
187 *oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP384R1 );
188 return( 0 );
Hanno Becker812e1242019-02-01 10:06:51 +0000189#endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */
190#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100191 case 521:
192 *oid = MBEDTLS_OID_EC_GRP_SECP521R1;
193 *oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP521R1 );
194 return( 0 );
Hanno Becker812e1242019-02-01 10:06:51 +0000195#endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Gilles Peskine89177e82019-12-03 21:19:09 +0100196 }
197 break;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100198 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine89177e82019-12-03 21:19:09 +0100199 switch( bits )
200 {
Hanno Becker812e1242019-02-01 10:06:51 +0000201#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100202 case 192:
203 *oid = MBEDTLS_OID_EC_GRP_SECP192K1;
204 *oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP192K1 );
205 return( 0 );
Hanno Becker812e1242019-02-01 10:06:51 +0000206#endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED */
207#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100208 case 224:
209 *oid = MBEDTLS_OID_EC_GRP_SECP224K1;
210 *oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP224K1 );
211 return( 0 );
Hanno Becker812e1242019-02-01 10:06:51 +0000212#endif /* MBEDTLS_ECP_DP_SECP224K1_ENABLED */
213#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100214 case 256:
215 *oid = MBEDTLS_OID_EC_GRP_SECP256K1;
216 *oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP256K1 );
217 return( 0 );
Hanno Becker812e1242019-02-01 10:06:51 +0000218#endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */
Gilles Peskine89177e82019-12-03 21:19:09 +0100219 }
220 break;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100221 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine89177e82019-12-03 21:19:09 +0100222 switch( bits )
223 {
Hanno Becker812e1242019-02-01 10:06:51 +0000224#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100225 case 256:
226 *oid = MBEDTLS_OID_EC_GRP_BP256R1;
227 *oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_BP256R1 );
228 return( 0 );
Hanno Becker812e1242019-02-01 10:06:51 +0000229#endif /* MBEDTLS_ECP_DP_BP256R1_ENABLED */
230#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100231 case 384:
232 *oid = MBEDTLS_OID_EC_GRP_BP384R1;
233 *oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_BP384R1 );
234 return( 0 );
Hanno Becker812e1242019-02-01 10:06:51 +0000235#endif /* MBEDTLS_ECP_DP_BP384R1_ENABLED */
236#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100237 case 512:
238 *oid = MBEDTLS_OID_EC_GRP_BP512R1;
239 *oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_BP512R1 );
240 return( 0 );
Hanno Becker812e1242019-02-01 10:06:51 +0000241#endif /* MBEDTLS_ECP_DP_BP512R1_ENABLED */
Gilles Peskine89177e82019-12-03 21:19:09 +0100242 }
243 break;
Hanno Becker812e1242019-02-01 10:06:51 +0000244 }
Gilles Peskine89177e82019-12-03 21:19:09 +0100245 (void) oid;
246 (void) oid_len;
247 return( -1 );
Hanno Becker812e1242019-02-01 10:06:51 +0000248}
249
Hanno Becker135baef2019-02-18 17:03:36 +0000250#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH 1
251
252#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
253#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 192 + 7 ) / 8 ) + 1 )
254#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
255#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 192 + 7 ) / 8 ) + 1 )
256#endif
257#endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */
258
259#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
260#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 224 + 7 ) / 8 ) + 1 )
261#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
262#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 224 + 7 ) / 8 ) + 1 )
263#endif
264#endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */
265
266#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
267#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 256 + 7 ) / 8 ) + 1 )
268#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
269#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 256 + 7 ) / 8 ) + 1 )
270#endif
271#endif /* MBEDTLS_ECP_DP_SECP256R1_ENABLED */
272
273#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
274#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 384 + 7 ) / 8 ) + 1 )
275#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
276#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 384 + 7 ) / 8 ) + 1 )
277#endif
278#endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */
279
280#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
281#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 521 + 7 ) / 8 ) + 1 )
282#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
283#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 521 + 7 ) / 8 ) + 1 )
284#endif
285#endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */
286
287#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
288#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 192 + 7 ) / 8 ) + 1 )
289#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
290#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 192 + 7 ) / 8 ) + 1 )
291#endif
292#endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED */
293
294#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
295#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 224 + 7 ) / 8 ) + 1 )
296#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
297#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 224 + 7 ) / 8 ) + 1 )
298#endif
299#endif /* MBEDTLS_ECP_DP_SECP224K1_ENABLED */
300
301#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
302#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 256 + 7 ) / 8 ) + 1 )
303#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
304#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 256 + 7 ) / 8 ) + 1 )
305#endif
306#endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */
307
308#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
309#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 256 + 7 ) / 8 ) + 1 )
310#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
311#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 256 + 7 ) / 8 ) + 1 )
312#endif
313#endif /* MBEDTLS_ECP_DP_BP256R1_ENABLED */
314
315#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
316#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 384 + 7 ) / 8 ) + 1 )
317#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
318#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 384 + 7 ) / 8 ) + 1 )
319#endif
320#endif /* MBEDTLS_ECP_DP_BP384R1_ENABLED */
321
322#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
323#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 512 + 7 ) / 8 ) + 1 )
324#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
325#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 512 + 7 ) / 8 ) + 1 )
326#endif
327#endif /* MBEDTLS_ECP_DP_BP512R1_ENABLED */
328
Hanno Beckerdf51dbe2019-02-18 16:41:55 +0000329
Hanno Becker000334f2018-11-15 09:37:19 +0000330/* Translations for PK layer */
331
332static inline int mbedtls_psa_err_translate_pk( psa_status_t status )
333{
334 switch( status )
335 {
336 case PSA_SUCCESS:
337 return( 0 );
338 case PSA_ERROR_NOT_SUPPORTED:
339 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
340 case PSA_ERROR_INSUFFICIENT_MEMORY:
341 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Hanno Becker000334f2018-11-15 09:37:19 +0000342 case PSA_ERROR_INSUFFICIENT_ENTROPY:
343 return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
344 case PSA_ERROR_BAD_STATE:
345 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Hanno Beckerf5f9ea22018-11-16 15:01:22 +0000346 /* All other failures */
347 case PSA_ERROR_COMMUNICATION_FAILURE:
348 case PSA_ERROR_HARDWARE_FAILURE:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200349 case PSA_ERROR_CORRUPTION_DETECTED:
TRodziewiczb579ccd2021-04-13 14:28:28 +0200350 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
Hanno Beckerf5f9ea22018-11-16 15:01:22 +0000351 default: /* We return the same as for the 'other failures',
352 * but list them separately nonetheless to indicate
353 * which failure conditions we have considered. */
TRodziewiczb579ccd2021-04-13 14:28:28 +0200354 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
Hanno Becker000334f2018-11-15 09:37:19 +0000355 }
356}
357
Andrzej Kurek93a38a32019-01-14 05:09:46 -0500358/* Translations for ECC */
359
360/* This function transforms an ECC group identifier from
361 * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8
362 * into a PSA ECC group identifier. */
Gilles Peskined8197cb2019-12-12 17:56:46 +0100363#if defined(MBEDTLS_ECP_C)
Gilles Peskined1959dc2019-12-18 20:44:49 +0100364static inline psa_key_type_t mbedtls_psa_parse_tls_ecc_group(
365 uint16_t tls_ecc_grp_reg_id, size_t *bits )
Andrzej Kurek93a38a32019-01-14 05:09:46 -0500366{
Gilles Peskined8197cb2019-12-12 17:56:46 +0100367 const mbedtls_ecp_curve_info *curve_info =
368 mbedtls_ecp_curve_info_from_tls_id( tls_ecc_grp_reg_id );
369 if( curve_info == NULL )
370 return( 0 );
Gilles Peskined1959dc2019-12-18 20:44:49 +0100371 return( PSA_KEY_TYPE_ECC_KEY_PAIR(
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200372 mbedtls_ecc_group_to_psa( curve_info->MBEDTLS_PRIVATE(grp_id), bits ) ) );
Andrzej Kurek93a38a32019-01-14 05:09:46 -0500373}
Gilles Peskined8197cb2019-12-12 17:56:46 +0100374#endif /* MBEDTLS_ECP_C */
Andrzej Kurek93a38a32019-01-14 05:09:46 -0500375
Hanno Beckerf75f9122019-01-07 15:36:51 +0000376/* This function takes a buffer holding an EC public key
377 * exported through psa_export_public_key(), and converts
378 * it into an ECPoint structure to be put into a ClientKeyExchange
379 * message in an ECDHE exchange.
380 *
381 * Both the present and the foreseeable future format of EC public keys
382 * used by PSA have the ECPoint structure contained in the exported key
383 * as a subbuffer, and the function merely selects this subbuffer instead
384 * of making a copy.
385 */
386static inline int mbedtls_psa_tls_psa_ec_to_ecpoint( unsigned char *src,
387 size_t srclen,
388 unsigned char **dst,
389 size_t *dstlen )
390{
391 *dst = src;
392 *dstlen = srclen;
393 return( 0 );
394}
395
396/* This function takes a buffer holding an ECPoint structure
397 * (as contained in a TLS ServerKeyExchange message for ECDHE
398 * exchanges) and converts it into a format that the PSA key
399 * agreement API understands.
400 */
Gilles Peskined1959dc2019-12-18 20:44:49 +0100401static inline int mbedtls_psa_tls_ecpoint_to_psa_ec( unsigned char const *src,
Hanno Beckerf75f9122019-01-07 15:36:51 +0000402 size_t srclen,
403 unsigned char *dst,
404 size_t dstlen,
405 size_t *olen )
406{
Hanno Beckerf75f9122019-01-07 15:36:51 +0000407 if( srclen > dstlen )
408 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
409
410 memcpy( dst, src, srclen );
411 *olen = srclen;
412 return( 0 );
413}
414
Hanno Becker87837b22018-11-08 13:32:02 +0000415#endif /* MBEDTLS_USE_PSA_CRYPTO */
416
Gilles Peskinee3ed8022021-02-03 20:04:08 +0100417/* Expose whatever RNG the PSA subsystem uses to applications using the
Gilles Peskine996f2162021-02-16 16:50:00 +0100418 * mbedtls_xxx API. The declarations and definitions here need to be
419 * consistent with the implementation in library/psa_crypto_random_impl.h.
420 * See that file for implementation documentation. */
Gilles Peskinee3ed8022021-02-03 20:04:08 +0100421#if defined(MBEDTLS_PSA_CRYPTO_C)
422
423/* The type of a `f_rng` random generator function that many library functions
424 * take.
425 *
426 * This type name is not part of the Mbed TLS stable API. It may be renamed
427 * or moved without warning.
428 */
429typedef int mbedtls_f_rng_t( void *p_rng, unsigned char *output, size_t output_size );
430
431#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
432
433/** The random generator function for the PSA subsystem.
434 *
435 * This function is suitable as the `f_rng` random generator function
Gilles Peskine2cff7e22021-02-16 16:49:42 +0100436 * parameter of many `mbedtls_xxx` functions. Use #MBEDTLS_PSA_RANDOM_STATE
437 * to obtain the \p p_rng parameter.
Gilles Peskinee3ed8022021-02-03 20:04:08 +0100438 *
439 * The implementation of this function depends on the configuration of the
440 * library.
Gilles Peskine2cff7e22021-02-16 16:49:42 +0100441 *
Gilles Peskinee3ed8022021-02-03 20:04:08 +0100442 * \note Depending on the configuration, this may be a function or
443 * a pointer to a function.
444 *
445 * \note This function may only be used if the PSA crypto subsystem is active.
446 * This means that you must call psa_crypto_init() before any call to
447 * this function, and you must not call this function after calling
448 * mbedtls_psa_crypto_free().
449 *
450 * \param p_rng The random generator context. This must be
451 * #MBEDTLS_PSA_RANDOM_STATE. No other state is
452 * supported.
453 * \param output The buffer to fill. It must have room for
454 * \c output_size bytes.
455 * \param output_size The number of bytes to write to \p output.
456 * This function may fail if \p output_size is too
457 * large. It is guaranteed to accept any output size
458 * requested by Mbed TLS library functions. The
459 * maximum request size depends on the library
460 * configuration.
461 *
462 * \return \c 0 on success.
463 * \return An `MBEDTLS_ERR_ENTROPY_xxx`,
Gilles Peskine2cff7e22021-02-16 16:49:42 +0100464 * `MBEDTLS_ERR_PLATFORM_xxx,
Gilles Peskinee3ed8022021-02-03 20:04:08 +0100465 * `MBEDTLS_ERR_CTR_DRBG_xxx` or
466 * `MBEDTLS_ERR_HMAC_DRBG_xxx` on error.
467 */
468int mbedtls_psa_get_random( void *p_rng,
469 unsigned char *output,
470 size_t output_size );
471
472/** The random generator state for the PSA subsystem.
473 *
474 * This macro expands to an expression which is suitable as the `p_rng`
475 * random generator state parameter of many `mbedtls_xxx` functions.
476 * It must be used in combination with the random generator function
477 * mbedtls_psa_get_random().
478 *
479 * The implementation of this macro depends on the configuration of the
480 * library. Do not make any assumption on its nature.
481 */
482#define MBEDTLS_PSA_RANDOM_STATE NULL
483
484#else /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
485
486#if defined(MBEDTLS_CTR_DRBG_C)
487#include "mbedtls/ctr_drbg.h"
488typedef mbedtls_ctr_drbg_context mbedtls_psa_drbg_context_t;
489static mbedtls_f_rng_t *const mbedtls_psa_get_random = mbedtls_ctr_drbg_random;
490#elif defined(MBEDTLS_HMAC_DRBG_C)
491#include "mbedtls/hmac_drbg.h"
492typedef mbedtls_hmac_drbg_context mbedtls_psa_drbg_context_t;
493static mbedtls_f_rng_t *const mbedtls_psa_get_random = mbedtls_hmac_drbg_random;
494#endif
495extern mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state;
496
497#define MBEDTLS_PSA_RANDOM_STATE mbedtls_psa_random_state
498
499#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
500
501#endif /* MBEDTLS_PSA_CRYPTO_C */
502
Hanno Becker186b65a2018-11-19 15:14:21 +0000503#endif /* MBEDTLS_PSA_UTIL_H */