blob: 1252280528a8f2022fbd79312fc3041ad4cda21e [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
Hanno Becker87837b22018-11-08 13:32:02 +000028
29#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010030#include "mbedtls/config.h"
Hanno Becker87837b22018-11-08 13:32:02 +000031#else
32#include MBEDTLS_CONFIG_FILE
33#endif
34
TRodziewiczb579ccd2021-04-13 14:28:28 +020035#if defined(MBEDTLS_PLATFORM_C)
36#include "mbedtls/platform.h"
37#endif
38
Hanno Becker87837b22018-11-08 13:32:02 +000039#if defined(MBEDTLS_USE_PSA_CRYPTO)
40
41#include "psa/crypto.h"
42
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010043#include "mbedtls/ecp.h"
44#include "mbedtls/md.h"
45#include "mbedtls/pk.h"
46#include "mbedtls/oid.h"
Hanno Becker87837b22018-11-08 13:32:02 +000047
Hanno Beckerf75f9122019-01-07 15:36:51 +000048#include <string.h>
49
Hanno Becker55251262018-11-12 09:29:12 +000050/* Translations for symmetric crypto. */
51
Hanno Beckerb26c1932018-11-12 10:18:57 +000052static inline psa_key_type_t mbedtls_psa_translate_cipher_type(
Hanno Becker55251262018-11-12 09:29:12 +000053 mbedtls_cipher_type_t cipher )
54{
55 switch( cipher )
56 {
57 case MBEDTLS_CIPHER_AES_128_CCM:
58 case MBEDTLS_CIPHER_AES_192_CCM:
59 case MBEDTLS_CIPHER_AES_256_CCM:
60 case MBEDTLS_CIPHER_AES_128_GCM:
61 case MBEDTLS_CIPHER_AES_192_GCM:
62 case MBEDTLS_CIPHER_AES_256_GCM:
63 case MBEDTLS_CIPHER_AES_128_CBC:
64 case MBEDTLS_CIPHER_AES_192_CBC:
65 case MBEDTLS_CIPHER_AES_256_CBC:
66 return( PSA_KEY_TYPE_AES );
67
68 /* ARIA not yet supported in PSA. */
69 /* case MBEDTLS_CIPHER_ARIA_128_CCM:
70 case MBEDTLS_CIPHER_ARIA_192_CCM:
71 case MBEDTLS_CIPHER_ARIA_256_CCM:
72 case MBEDTLS_CIPHER_ARIA_128_GCM:
73 case MBEDTLS_CIPHER_ARIA_192_GCM:
74 case MBEDTLS_CIPHER_ARIA_256_GCM:
75 case MBEDTLS_CIPHER_ARIA_128_CBC:
76 case MBEDTLS_CIPHER_ARIA_192_CBC:
77 case MBEDTLS_CIPHER_ARIA_256_CBC:
78 return( PSA_KEY_TYPE_ARIA ); */
79
80 default:
81 return( 0 );
82 }
83}
84
Hanno Beckerb26c1932018-11-12 10:18:57 +000085static inline psa_algorithm_t mbedtls_psa_translate_cipher_mode(
Hanno Becker010cf7e2018-11-15 15:48:57 +000086 mbedtls_cipher_mode_t mode, size_t taglen )
Hanno Becker55251262018-11-12 09:29:12 +000087{
88 switch( mode )
89 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +020090 case MBEDTLS_MODE_ECB:
Steven Cooremana6033e92020-08-25 11:47:50 +020091 return( PSA_ALG_ECB_NO_PADDING );
Hanno Becker55251262018-11-12 09:29:12 +000092 case MBEDTLS_MODE_GCM:
Bence Szépkútia63b20d2020-12-16 11:36:46 +010093 return( PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, taglen ) );
Hanno Becker55251262018-11-12 09:29:12 +000094 case MBEDTLS_MODE_CCM:
Bence Szépkútia63b20d2020-12-16 11:36:46 +010095 return( PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) );
Hanno Becker55251262018-11-12 09:29:12 +000096 case MBEDTLS_MODE_CBC:
Hanno Becker010cf7e2018-11-15 15:48:57 +000097 if( taglen == 0 )
98 return( PSA_ALG_CBC_NO_PADDING );
Paul Elliott36f539d2021-03-09 16:51:02 +000099 else
100 return( 0 );
Hanno Becker55251262018-11-12 09:29:12 +0000101 default:
102 return( 0 );
103 }
104}
105
Hanno Beckerb26c1932018-11-12 10:18:57 +0000106static inline psa_key_usage_t mbedtls_psa_translate_cipher_operation(
107 mbedtls_operation_t op )
Hanno Becker55251262018-11-12 09:29:12 +0000108{
109 switch( op )
110 {
111 case MBEDTLS_ENCRYPT:
112 return( PSA_KEY_USAGE_ENCRYPT );
113 case MBEDTLS_DECRYPT:
114 return( PSA_KEY_USAGE_DECRYPT );
115 default:
116 return( 0 );
117 }
118}
119
120/* Translations for hashing. */
121
Hanno Beckerb26c1932018-11-12 10:18:57 +0000122static inline psa_algorithm_t mbedtls_psa_translate_md( mbedtls_md_type_t md_alg )
Hanno Becker87837b22018-11-08 13:32:02 +0000123{
124 switch( md_alg )
125 {
126#if defined(MBEDTLS_MD2_C)
127 case MBEDTLS_MD_MD2:
128 return( PSA_ALG_MD2 );
129#endif
130#if defined(MBEDTLS_MD4_C)
131 case MBEDTLS_MD_MD4:
132 return( PSA_ALG_MD4 );
133#endif
134#if defined(MBEDTLS_MD5_C)
135 case MBEDTLS_MD_MD5:
136 return( PSA_ALG_MD5 );
137#endif
138#if defined(MBEDTLS_SHA1_C)
139 case MBEDTLS_MD_SHA1:
140 return( PSA_ALG_SHA_1 );
141#endif
142#if defined(MBEDTLS_SHA256_C)
143 case MBEDTLS_MD_SHA224:
144 return( PSA_ALG_SHA_224 );
145 case MBEDTLS_MD_SHA256:
146 return( PSA_ALG_SHA_256 );
147#endif
148#if defined(MBEDTLS_SHA512_C)
149 case MBEDTLS_MD_SHA384:
150 return( PSA_ALG_SHA_384 );
151 case MBEDTLS_MD_SHA512:
152 return( PSA_ALG_SHA_512 );
153#endif
154#if defined(MBEDTLS_RIPEMD160_C)
155 case MBEDTLS_MD_RIPEMD160:
156 return( PSA_ALG_RIPEMD160 );
157#endif
Paul Elliott36f539d2021-03-09 16:51:02 +0000158 case MBEDTLS_MD_NONE:
159 return( 0 );
Hanno Becker87837b22018-11-08 13:32:02 +0000160 default:
161 return( 0 );
162 }
163}
164
Hanno Becker55251262018-11-12 09:29:12 +0000165/* Translations for ECC. */
166
Hanno Becker812e1242019-02-01 10:06:51 +0000167static inline int mbedtls_psa_get_ecc_oid_from_id(
Paul Elliott8ff510a2020-06-02 17:19:28 +0100168 psa_ecc_family_t curve, size_t bits,
Gilles Peskine89177e82019-12-03 21:19:09 +0100169 char const **oid, size_t *oid_len )
Hanno Becker812e1242019-02-01 10:06:51 +0000170{
171 switch( curve )
172 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100173 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine89177e82019-12-03 21:19:09 +0100174 switch( bits )
175 {
Hanno Becker812e1242019-02-01 10:06:51 +0000176#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100177 case 192:
178 *oid = MBEDTLS_OID_EC_GRP_SECP192R1;
179 *oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP192R1 );
180 return( 0 );
Hanno Becker812e1242019-02-01 10:06:51 +0000181#endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */
182#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100183 case 224:
184 *oid = MBEDTLS_OID_EC_GRP_SECP224R1;
185 *oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP224R1 );
186 return( 0 );
Hanno Becker812e1242019-02-01 10:06:51 +0000187#endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */
188#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100189 case 256:
190 *oid = MBEDTLS_OID_EC_GRP_SECP256R1;
191 *oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP256R1 );
192 return( 0 );
Hanno Becker812e1242019-02-01 10:06:51 +0000193#endif /* MBEDTLS_ECP_DP_SECP256R1_ENABLED */
194#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100195 case 384:
196 *oid = MBEDTLS_OID_EC_GRP_SECP384R1;
197 *oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP384R1 );
198 return( 0 );
Hanno Becker812e1242019-02-01 10:06:51 +0000199#endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */
200#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100201 case 521:
202 *oid = MBEDTLS_OID_EC_GRP_SECP521R1;
203 *oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP521R1 );
204 return( 0 );
Hanno Becker812e1242019-02-01 10:06:51 +0000205#endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Gilles Peskine89177e82019-12-03 21:19:09 +0100206 }
207 break;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100208 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine89177e82019-12-03 21:19:09 +0100209 switch( bits )
210 {
Hanno Becker812e1242019-02-01 10:06:51 +0000211#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100212 case 192:
213 *oid = MBEDTLS_OID_EC_GRP_SECP192K1;
214 *oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP192K1 );
215 return( 0 );
Hanno Becker812e1242019-02-01 10:06:51 +0000216#endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED */
217#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100218 case 224:
219 *oid = MBEDTLS_OID_EC_GRP_SECP224K1;
220 *oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP224K1 );
221 return( 0 );
Hanno Becker812e1242019-02-01 10:06:51 +0000222#endif /* MBEDTLS_ECP_DP_SECP224K1_ENABLED */
223#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100224 case 256:
225 *oid = MBEDTLS_OID_EC_GRP_SECP256K1;
226 *oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP256K1 );
227 return( 0 );
Hanno Becker812e1242019-02-01 10:06:51 +0000228#endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */
Gilles Peskine89177e82019-12-03 21:19:09 +0100229 }
230 break;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100231 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine89177e82019-12-03 21:19:09 +0100232 switch( bits )
233 {
Hanno Becker812e1242019-02-01 10:06:51 +0000234#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100235 case 256:
236 *oid = MBEDTLS_OID_EC_GRP_BP256R1;
237 *oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_BP256R1 );
238 return( 0 );
Hanno Becker812e1242019-02-01 10:06:51 +0000239#endif /* MBEDTLS_ECP_DP_BP256R1_ENABLED */
240#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100241 case 384:
242 *oid = MBEDTLS_OID_EC_GRP_BP384R1;
243 *oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_BP384R1 );
244 return( 0 );
Hanno Becker812e1242019-02-01 10:06:51 +0000245#endif /* MBEDTLS_ECP_DP_BP384R1_ENABLED */
246#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100247 case 512:
248 *oid = MBEDTLS_OID_EC_GRP_BP512R1;
249 *oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_BP512R1 );
250 return( 0 );
Hanno Becker812e1242019-02-01 10:06:51 +0000251#endif /* MBEDTLS_ECP_DP_BP512R1_ENABLED */
Gilles Peskine89177e82019-12-03 21:19:09 +0100252 }
253 break;
Hanno Becker812e1242019-02-01 10:06:51 +0000254 }
Gilles Peskine89177e82019-12-03 21:19:09 +0100255 (void) oid;
256 (void) oid_len;
257 return( -1 );
Hanno Becker812e1242019-02-01 10:06:51 +0000258}
259
Hanno Becker135baef2019-02-18 17:03:36 +0000260#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH 1
261
262#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
263#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 192 + 7 ) / 8 ) + 1 )
264#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
265#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 192 + 7 ) / 8 ) + 1 )
266#endif
267#endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */
268
269#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
270#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 224 + 7 ) / 8 ) + 1 )
271#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
272#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 224 + 7 ) / 8 ) + 1 )
273#endif
274#endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */
275
276#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
277#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 256 + 7 ) / 8 ) + 1 )
278#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
279#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 256 + 7 ) / 8 ) + 1 )
280#endif
281#endif /* MBEDTLS_ECP_DP_SECP256R1_ENABLED */
282
283#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
284#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 384 + 7 ) / 8 ) + 1 )
285#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
286#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 384 + 7 ) / 8 ) + 1 )
287#endif
288#endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */
289
290#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
291#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 521 + 7 ) / 8 ) + 1 )
292#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
293#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 521 + 7 ) / 8 ) + 1 )
294#endif
295#endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */
296
297#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
298#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 192 + 7 ) / 8 ) + 1 )
299#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
300#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 192 + 7 ) / 8 ) + 1 )
301#endif
302#endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED */
303
304#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
305#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 224 + 7 ) / 8 ) + 1 )
306#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
307#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 224 + 7 ) / 8 ) + 1 )
308#endif
309#endif /* MBEDTLS_ECP_DP_SECP224K1_ENABLED */
310
311#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
312#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 256 + 7 ) / 8 ) + 1 )
313#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
314#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 256 + 7 ) / 8 ) + 1 )
315#endif
316#endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */
317
318#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
319#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 256 + 7 ) / 8 ) + 1 )
320#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
321#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 256 + 7 ) / 8 ) + 1 )
322#endif
323#endif /* MBEDTLS_ECP_DP_BP256R1_ENABLED */
324
325#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
326#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 384 + 7 ) / 8 ) + 1 )
327#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
328#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 384 + 7 ) / 8 ) + 1 )
329#endif
330#endif /* MBEDTLS_ECP_DP_BP384R1_ENABLED */
331
332#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
333#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 512 + 7 ) / 8 ) + 1 )
334#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
335#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 512 + 7 ) / 8 ) + 1 )
336#endif
337#endif /* MBEDTLS_ECP_DP_BP512R1_ENABLED */
338
Hanno Beckerdf51dbe2019-02-18 16:41:55 +0000339
Hanno Becker000334f2018-11-15 09:37:19 +0000340/* Translations for PK layer */
341
342static inline int mbedtls_psa_err_translate_pk( psa_status_t status )
343{
344 switch( status )
345 {
346 case PSA_SUCCESS:
347 return( 0 );
348 case PSA_ERROR_NOT_SUPPORTED:
349 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
350 case PSA_ERROR_INSUFFICIENT_MEMORY:
351 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Hanno Becker000334f2018-11-15 09:37:19 +0000352 case PSA_ERROR_INSUFFICIENT_ENTROPY:
353 return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
354 case PSA_ERROR_BAD_STATE:
355 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Hanno Beckerf5f9ea22018-11-16 15:01:22 +0000356 /* All other failures */
357 case PSA_ERROR_COMMUNICATION_FAILURE:
358 case PSA_ERROR_HARDWARE_FAILURE:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200359 case PSA_ERROR_CORRUPTION_DETECTED:
TRodziewiczb579ccd2021-04-13 14:28:28 +0200360 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
Hanno Beckerf5f9ea22018-11-16 15:01:22 +0000361 default: /* We return the same as for the 'other failures',
362 * but list them separately nonetheless to indicate
363 * which failure conditions we have considered. */
TRodziewiczb579ccd2021-04-13 14:28:28 +0200364 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
Hanno Becker000334f2018-11-15 09:37:19 +0000365 }
366}
367
Andrzej Kurek93a38a32019-01-14 05:09:46 -0500368/* Translations for ECC */
369
370/* This function transforms an ECC group identifier from
371 * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8
372 * into a PSA ECC group identifier. */
Gilles Peskined8197cb2019-12-12 17:56:46 +0100373#if defined(MBEDTLS_ECP_C)
Gilles Peskined1959dc2019-12-18 20:44:49 +0100374static inline psa_key_type_t mbedtls_psa_parse_tls_ecc_group(
375 uint16_t tls_ecc_grp_reg_id, size_t *bits )
Andrzej Kurek93a38a32019-01-14 05:09:46 -0500376{
Gilles Peskined8197cb2019-12-12 17:56:46 +0100377 const mbedtls_ecp_curve_info *curve_info =
378 mbedtls_ecp_curve_info_from_tls_id( tls_ecc_grp_reg_id );
379 if( curve_info == NULL )
380 return( 0 );
Gilles Peskined1959dc2019-12-18 20:44:49 +0100381 return( PSA_KEY_TYPE_ECC_KEY_PAIR(
382 mbedtls_ecc_group_to_psa( curve_info->grp_id, bits ) ) );
Andrzej Kurek93a38a32019-01-14 05:09:46 -0500383}
Gilles Peskined8197cb2019-12-12 17:56:46 +0100384#endif /* MBEDTLS_ECP_C */
Andrzej Kurek93a38a32019-01-14 05:09:46 -0500385
Hanno Beckerf75f9122019-01-07 15:36:51 +0000386/* This function takes a buffer holding an EC public key
387 * exported through psa_export_public_key(), and converts
388 * it into an ECPoint structure to be put into a ClientKeyExchange
389 * message in an ECDHE exchange.
390 *
391 * Both the present and the foreseeable future format of EC public keys
392 * used by PSA have the ECPoint structure contained in the exported key
393 * as a subbuffer, and the function merely selects this subbuffer instead
394 * of making a copy.
395 */
396static inline int mbedtls_psa_tls_psa_ec_to_ecpoint( unsigned char *src,
397 size_t srclen,
398 unsigned char **dst,
399 size_t *dstlen )
400{
401 *dst = src;
402 *dstlen = srclen;
403 return( 0 );
404}
405
406/* This function takes a buffer holding an ECPoint structure
407 * (as contained in a TLS ServerKeyExchange message for ECDHE
408 * exchanges) and converts it into a format that the PSA key
409 * agreement API understands.
410 */
Gilles Peskined1959dc2019-12-18 20:44:49 +0100411static inline int mbedtls_psa_tls_ecpoint_to_psa_ec( unsigned char const *src,
Hanno Beckerf75f9122019-01-07 15:36:51 +0000412 size_t srclen,
413 unsigned char *dst,
414 size_t dstlen,
415 size_t *olen )
416{
Hanno Beckerf75f9122019-01-07 15:36:51 +0000417 if( srclen > dstlen )
418 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
419
420 memcpy( dst, src, srclen );
421 *olen = srclen;
422 return( 0 );
423}
424
Hanno Becker87837b22018-11-08 13:32:02 +0000425#endif /* MBEDTLS_USE_PSA_CRYPTO */
426
Gilles Peskinee3ed8022021-02-03 20:04:08 +0100427/* Expose whatever RNG the PSA subsystem uses to applications using the
Gilles Peskine996f2162021-02-16 16:50:00 +0100428 * mbedtls_xxx API. The declarations and definitions here need to be
429 * consistent with the implementation in library/psa_crypto_random_impl.h.
430 * See that file for implementation documentation. */
Gilles Peskinee3ed8022021-02-03 20:04:08 +0100431#if defined(MBEDTLS_PSA_CRYPTO_C)
432
433/* The type of a `f_rng` random generator function that many library functions
434 * take.
435 *
436 * This type name is not part of the Mbed TLS stable API. It may be renamed
437 * or moved without warning.
438 */
439typedef int mbedtls_f_rng_t( void *p_rng, unsigned char *output, size_t output_size );
440
441#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
442
443/** The random generator function for the PSA subsystem.
444 *
445 * This function is suitable as the `f_rng` random generator function
Gilles Peskine2cff7e22021-02-16 16:49:42 +0100446 * parameter of many `mbedtls_xxx` functions. Use #MBEDTLS_PSA_RANDOM_STATE
447 * to obtain the \p p_rng parameter.
Gilles Peskinee3ed8022021-02-03 20:04:08 +0100448 *
449 * The implementation of this function depends on the configuration of the
450 * library.
Gilles Peskine2cff7e22021-02-16 16:49:42 +0100451 *
Gilles Peskinee3ed8022021-02-03 20:04:08 +0100452 * \note Depending on the configuration, this may be a function or
453 * a pointer to a function.
454 *
455 * \note This function may only be used if the PSA crypto subsystem is active.
456 * This means that you must call psa_crypto_init() before any call to
457 * this function, and you must not call this function after calling
458 * mbedtls_psa_crypto_free().
459 *
460 * \param p_rng The random generator context. This must be
461 * #MBEDTLS_PSA_RANDOM_STATE. No other state is
462 * supported.
463 * \param output The buffer to fill. It must have room for
464 * \c output_size bytes.
465 * \param output_size The number of bytes to write to \p output.
466 * This function may fail if \p output_size is too
467 * large. It is guaranteed to accept any output size
468 * requested by Mbed TLS library functions. The
469 * maximum request size depends on the library
470 * configuration.
471 *
472 * \return \c 0 on success.
473 * \return An `MBEDTLS_ERR_ENTROPY_xxx`,
Gilles Peskine2cff7e22021-02-16 16:49:42 +0100474 * `MBEDTLS_ERR_PLATFORM_xxx,
Gilles Peskinee3ed8022021-02-03 20:04:08 +0100475 * `MBEDTLS_ERR_CTR_DRBG_xxx` or
476 * `MBEDTLS_ERR_HMAC_DRBG_xxx` on error.
477 */
478int mbedtls_psa_get_random( void *p_rng,
479 unsigned char *output,
480 size_t output_size );
481
482/** The random generator state for the PSA subsystem.
483 *
484 * This macro expands to an expression which is suitable as the `p_rng`
485 * random generator state parameter of many `mbedtls_xxx` functions.
486 * It must be used in combination with the random generator function
487 * mbedtls_psa_get_random().
488 *
489 * The implementation of this macro depends on the configuration of the
490 * library. Do not make any assumption on its nature.
491 */
492#define MBEDTLS_PSA_RANDOM_STATE NULL
493
494#else /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
495
496#if defined(MBEDTLS_CTR_DRBG_C)
497#include "mbedtls/ctr_drbg.h"
498typedef mbedtls_ctr_drbg_context mbedtls_psa_drbg_context_t;
499static mbedtls_f_rng_t *const mbedtls_psa_get_random = mbedtls_ctr_drbg_random;
500#elif defined(MBEDTLS_HMAC_DRBG_C)
501#include "mbedtls/hmac_drbg.h"
502typedef mbedtls_hmac_drbg_context mbedtls_psa_drbg_context_t;
503static mbedtls_f_rng_t *const mbedtls_psa_get_random = mbedtls_hmac_drbg_random;
504#endif
505extern mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state;
506
507#define MBEDTLS_PSA_RANDOM_STATE mbedtls_psa_random_state
508
509#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
510
511#endif /* MBEDTLS_PSA_CRYPTO_C */
512
Hanno Becker186b65a2018-11-19 15:14:21 +0000513#endif /* MBEDTLS_PSA_UTIL_H */