blob: 31a1254b9fa78832aee5bec9f9e8d494092a1305 [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:
Przemyslaw Stekiel80c6a8e2021-09-29 12:13:11 +020060 case MBEDTLS_CIPHER_AES_128_ECB:
Hanno Becker55251262018-11-12 09:29:12 +000061 return( PSA_KEY_TYPE_AES );
62
63 /* ARIA not yet supported in PSA. */
64 /* case MBEDTLS_CIPHER_ARIA_128_CCM:
65 case MBEDTLS_CIPHER_ARIA_192_CCM:
66 case MBEDTLS_CIPHER_ARIA_256_CCM:
67 case MBEDTLS_CIPHER_ARIA_128_GCM:
68 case MBEDTLS_CIPHER_ARIA_192_GCM:
69 case MBEDTLS_CIPHER_ARIA_256_GCM:
70 case MBEDTLS_CIPHER_ARIA_128_CBC:
71 case MBEDTLS_CIPHER_ARIA_192_CBC:
72 case MBEDTLS_CIPHER_ARIA_256_CBC:
73 return( PSA_KEY_TYPE_ARIA ); */
74
75 default:
76 return( 0 );
77 }
78}
79
Hanno Beckerb26c1932018-11-12 10:18:57 +000080static inline psa_algorithm_t mbedtls_psa_translate_cipher_mode(
Hanno Becker010cf7e2018-11-15 15:48:57 +000081 mbedtls_cipher_mode_t mode, size_t taglen )
Hanno Becker55251262018-11-12 09:29:12 +000082{
83 switch( mode )
84 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +020085 case MBEDTLS_MODE_ECB:
Steven Cooremana6033e92020-08-25 11:47:50 +020086 return( PSA_ALG_ECB_NO_PADDING );
Hanno Becker55251262018-11-12 09:29:12 +000087 case MBEDTLS_MODE_GCM:
Bence Szépkútia63b20d2020-12-16 11:36:46 +010088 return( PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, taglen ) );
Hanno Becker55251262018-11-12 09:29:12 +000089 case MBEDTLS_MODE_CCM:
Bence Szépkútia63b20d2020-12-16 11:36:46 +010090 return( PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) );
Hanno Becker55251262018-11-12 09:29:12 +000091 case MBEDTLS_MODE_CBC:
Hanno Becker010cf7e2018-11-15 15:48:57 +000092 if( taglen == 0 )
93 return( PSA_ALG_CBC_NO_PADDING );
Paul Elliott36f539d2021-03-09 16:51:02 +000094 else
95 return( 0 );
Hanno Becker55251262018-11-12 09:29:12 +000096 default:
97 return( 0 );
98 }
99}
100
Hanno Beckerb26c1932018-11-12 10:18:57 +0000101static inline psa_key_usage_t mbedtls_psa_translate_cipher_operation(
102 mbedtls_operation_t op )
Hanno Becker55251262018-11-12 09:29:12 +0000103{
104 switch( op )
105 {
106 case MBEDTLS_ENCRYPT:
107 return( PSA_KEY_USAGE_ENCRYPT );
108 case MBEDTLS_DECRYPT:
109 return( PSA_KEY_USAGE_DECRYPT );
110 default:
111 return( 0 );
112 }
113}
114
115/* Translations for hashing. */
116
Hanno Beckerb26c1932018-11-12 10:18:57 +0000117static inline psa_algorithm_t mbedtls_psa_translate_md( mbedtls_md_type_t md_alg )
Hanno Becker87837b22018-11-08 13:32:02 +0000118{
119 switch( md_alg )
120 {
Hanno Becker87837b22018-11-08 13:32:02 +0000121#if defined(MBEDTLS_MD5_C)
122 case MBEDTLS_MD_MD5:
123 return( PSA_ALG_MD5 );
124#endif
125#if defined(MBEDTLS_SHA1_C)
126 case MBEDTLS_MD_SHA1:
127 return( PSA_ALG_SHA_1 );
128#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200129#if defined(MBEDTLS_SHA224_C)
Hanno Becker87837b22018-11-08 13:32:02 +0000130 case MBEDTLS_MD_SHA224:
131 return( PSA_ALG_SHA_224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200132#endif
133#if defined(MBEDTLS_SHA256_C)
Hanno Becker87837b22018-11-08 13:32:02 +0000134 case MBEDTLS_MD_SHA256:
135 return( PSA_ALG_SHA_256 );
136#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200137#if defined(MBEDTLS_SHA384_C)
Hanno Becker87837b22018-11-08 13:32:02 +0000138 case MBEDTLS_MD_SHA384:
139 return( PSA_ALG_SHA_384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200140#endif
141#if defined(MBEDTLS_SHA512_C)
Hanno Becker87837b22018-11-08 13:32:02 +0000142 case MBEDTLS_MD_SHA512:
143 return( PSA_ALG_SHA_512 );
144#endif
145#if defined(MBEDTLS_RIPEMD160_C)
146 case MBEDTLS_MD_RIPEMD160:
147 return( PSA_ALG_RIPEMD160 );
148#endif
Paul Elliott36f539d2021-03-09 16:51:02 +0000149 case MBEDTLS_MD_NONE:
150 return( 0 );
Hanno Becker87837b22018-11-08 13:32:02 +0000151 default:
152 return( 0 );
153 }
154}
155
Hanno Becker55251262018-11-12 09:29:12 +0000156/* Translations for ECC. */
157
Hanno Becker812e1242019-02-01 10:06:51 +0000158static inline int mbedtls_psa_get_ecc_oid_from_id(
Paul Elliott8ff510a2020-06-02 17:19:28 +0100159 psa_ecc_family_t curve, size_t bits,
Gilles Peskine89177e82019-12-03 21:19:09 +0100160 char const **oid, size_t *oid_len )
Hanno Becker812e1242019-02-01 10:06:51 +0000161{
162 switch( curve )
163 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100164 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine89177e82019-12-03 21:19:09 +0100165 switch( bits )
166 {
Hanno Becker812e1242019-02-01 10:06:51 +0000167#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100168 case 192:
169 *oid = MBEDTLS_OID_EC_GRP_SECP192R1;
170 *oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP192R1 );
171 return( 0 );
Hanno Becker812e1242019-02-01 10:06:51 +0000172#endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */
173#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100174 case 224:
175 *oid = MBEDTLS_OID_EC_GRP_SECP224R1;
176 *oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP224R1 );
177 return( 0 );
Hanno Becker812e1242019-02-01 10:06:51 +0000178#endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */
179#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100180 case 256:
181 *oid = MBEDTLS_OID_EC_GRP_SECP256R1;
182 *oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP256R1 );
183 return( 0 );
Hanno Becker812e1242019-02-01 10:06:51 +0000184#endif /* MBEDTLS_ECP_DP_SECP256R1_ENABLED */
185#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100186 case 384:
187 *oid = MBEDTLS_OID_EC_GRP_SECP384R1;
188 *oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP384R1 );
189 return( 0 );
Hanno Becker812e1242019-02-01 10:06:51 +0000190#endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */
191#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100192 case 521:
193 *oid = MBEDTLS_OID_EC_GRP_SECP521R1;
194 *oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP521R1 );
195 return( 0 );
Hanno Becker812e1242019-02-01 10:06:51 +0000196#endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Gilles Peskine89177e82019-12-03 21:19:09 +0100197 }
198 break;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100199 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine89177e82019-12-03 21:19:09 +0100200 switch( bits )
201 {
Hanno Becker812e1242019-02-01 10:06:51 +0000202#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100203 case 192:
204 *oid = MBEDTLS_OID_EC_GRP_SECP192K1;
205 *oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP192K1 );
206 return( 0 );
Hanno Becker812e1242019-02-01 10:06:51 +0000207#endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED */
208#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100209 case 224:
210 *oid = MBEDTLS_OID_EC_GRP_SECP224K1;
211 *oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP224K1 );
212 return( 0 );
Hanno Becker812e1242019-02-01 10:06:51 +0000213#endif /* MBEDTLS_ECP_DP_SECP224K1_ENABLED */
214#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100215 case 256:
216 *oid = MBEDTLS_OID_EC_GRP_SECP256K1;
217 *oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP256K1 );
218 return( 0 );
Hanno Becker812e1242019-02-01 10:06:51 +0000219#endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */
Gilles Peskine89177e82019-12-03 21:19:09 +0100220 }
221 break;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100222 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine89177e82019-12-03 21:19:09 +0100223 switch( bits )
224 {
Hanno Becker812e1242019-02-01 10:06:51 +0000225#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100226 case 256:
227 *oid = MBEDTLS_OID_EC_GRP_BP256R1;
228 *oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_BP256R1 );
229 return( 0 );
Hanno Becker812e1242019-02-01 10:06:51 +0000230#endif /* MBEDTLS_ECP_DP_BP256R1_ENABLED */
231#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100232 case 384:
233 *oid = MBEDTLS_OID_EC_GRP_BP384R1;
234 *oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_BP384R1 );
235 return( 0 );
Hanno Becker812e1242019-02-01 10:06:51 +0000236#endif /* MBEDTLS_ECP_DP_BP384R1_ENABLED */
237#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100238 case 512:
239 *oid = MBEDTLS_OID_EC_GRP_BP512R1;
240 *oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_BP512R1 );
241 return( 0 );
Hanno Becker812e1242019-02-01 10:06:51 +0000242#endif /* MBEDTLS_ECP_DP_BP512R1_ENABLED */
Gilles Peskine89177e82019-12-03 21:19:09 +0100243 }
244 break;
Hanno Becker812e1242019-02-01 10:06:51 +0000245 }
Gilles Peskine89177e82019-12-03 21:19:09 +0100246 (void) oid;
247 (void) oid_len;
248 return( -1 );
Hanno Becker812e1242019-02-01 10:06:51 +0000249}
250
Hanno Becker135baef2019-02-18 17:03:36 +0000251#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH 1
252
253#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
254#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 192 + 7 ) / 8 ) + 1 )
255#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
256#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 192 + 7 ) / 8 ) + 1 )
257#endif
258#endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */
259
260#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
261#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 224 + 7 ) / 8 ) + 1 )
262#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
263#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 224 + 7 ) / 8 ) + 1 )
264#endif
265#endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */
266
267#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
268#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 256 + 7 ) / 8 ) + 1 )
269#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
270#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 256 + 7 ) / 8 ) + 1 )
271#endif
272#endif /* MBEDTLS_ECP_DP_SECP256R1_ENABLED */
273
274#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
275#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 384 + 7 ) / 8 ) + 1 )
276#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
277#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 384 + 7 ) / 8 ) + 1 )
278#endif
279#endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */
280
281#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
282#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 521 + 7 ) / 8 ) + 1 )
283#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
284#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 521 + 7 ) / 8 ) + 1 )
285#endif
286#endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */
287
288#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
289#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 192 + 7 ) / 8 ) + 1 )
290#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
291#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 192 + 7 ) / 8 ) + 1 )
292#endif
293#endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED */
294
295#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
296#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 224 + 7 ) / 8 ) + 1 )
297#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
298#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 224 + 7 ) / 8 ) + 1 )
299#endif
300#endif /* MBEDTLS_ECP_DP_SECP224K1_ENABLED */
301
302#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
303#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 256 + 7 ) / 8 ) + 1 )
304#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
305#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 256 + 7 ) / 8 ) + 1 )
306#endif
307#endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */
308
309#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
310#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 256 + 7 ) / 8 ) + 1 )
311#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
312#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 256 + 7 ) / 8 ) + 1 )
313#endif
314#endif /* MBEDTLS_ECP_DP_BP256R1_ENABLED */
315
316#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
317#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 384 + 7 ) / 8 ) + 1 )
318#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
319#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 384 + 7 ) / 8 ) + 1 )
320#endif
321#endif /* MBEDTLS_ECP_DP_BP384R1_ENABLED */
322
323#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
324#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 512 + 7 ) / 8 ) + 1 )
325#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
326#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 512 + 7 ) / 8 ) + 1 )
327#endif
328#endif /* MBEDTLS_ECP_DP_BP512R1_ENABLED */
329
Hanno Beckerdf51dbe2019-02-18 16:41:55 +0000330
Hanno Becker000334f2018-11-15 09:37:19 +0000331/* Translations for PK layer */
332
333static inline int mbedtls_psa_err_translate_pk( psa_status_t status )
334{
335 switch( status )
336 {
337 case PSA_SUCCESS:
338 return( 0 );
339 case PSA_ERROR_NOT_SUPPORTED:
340 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
341 case PSA_ERROR_INSUFFICIENT_MEMORY:
342 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Hanno Becker000334f2018-11-15 09:37:19 +0000343 case PSA_ERROR_INSUFFICIENT_ENTROPY:
344 return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
345 case PSA_ERROR_BAD_STATE:
346 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Hanno Beckerf5f9ea22018-11-16 15:01:22 +0000347 /* All other failures */
348 case PSA_ERROR_COMMUNICATION_FAILURE:
349 case PSA_ERROR_HARDWARE_FAILURE:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200350 case PSA_ERROR_CORRUPTION_DETECTED:
TRodziewiczb579ccd2021-04-13 14:28:28 +0200351 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
Hanno Beckerf5f9ea22018-11-16 15:01:22 +0000352 default: /* We return the same as for the 'other failures',
353 * but list them separately nonetheless to indicate
354 * which failure conditions we have considered. */
TRodziewiczb579ccd2021-04-13 14:28:28 +0200355 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
Hanno Becker000334f2018-11-15 09:37:19 +0000356 }
357}
358
Andrzej Kurek93a38a32019-01-14 05:09:46 -0500359/* Translations for ECC */
360
361/* This function transforms an ECC group identifier from
362 * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8
363 * into a PSA ECC group identifier. */
Gilles Peskined8197cb2019-12-12 17:56:46 +0100364#if defined(MBEDTLS_ECP_C)
Gilles Peskined1959dc2019-12-18 20:44:49 +0100365static inline psa_key_type_t mbedtls_psa_parse_tls_ecc_group(
366 uint16_t tls_ecc_grp_reg_id, size_t *bits )
Andrzej Kurek93a38a32019-01-14 05:09:46 -0500367{
Gilles Peskined8197cb2019-12-12 17:56:46 +0100368 const mbedtls_ecp_curve_info *curve_info =
369 mbedtls_ecp_curve_info_from_tls_id( tls_ecc_grp_reg_id );
370 if( curve_info == NULL )
371 return( 0 );
Gilles Peskined1959dc2019-12-18 20:44:49 +0100372 return( PSA_KEY_TYPE_ECC_KEY_PAIR(
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200373 mbedtls_ecc_group_to_psa( curve_info->MBEDTLS_PRIVATE(grp_id), bits ) ) );
Andrzej Kurek93a38a32019-01-14 05:09:46 -0500374}
Gilles Peskined8197cb2019-12-12 17:56:46 +0100375#endif /* MBEDTLS_ECP_C */
Andrzej Kurek93a38a32019-01-14 05:09:46 -0500376
Hanno Beckerf75f9122019-01-07 15:36:51 +0000377/* This function takes a buffer holding an EC public key
378 * exported through psa_export_public_key(), and converts
379 * it into an ECPoint structure to be put into a ClientKeyExchange
380 * message in an ECDHE exchange.
381 *
382 * Both the present and the foreseeable future format of EC public keys
383 * used by PSA have the ECPoint structure contained in the exported key
384 * as a subbuffer, and the function merely selects this subbuffer instead
385 * of making a copy.
386 */
387static inline int mbedtls_psa_tls_psa_ec_to_ecpoint( unsigned char *src,
388 size_t srclen,
389 unsigned char **dst,
390 size_t *dstlen )
391{
392 *dst = src;
393 *dstlen = srclen;
394 return( 0 );
395}
396
397/* This function takes a buffer holding an ECPoint structure
398 * (as contained in a TLS ServerKeyExchange message for ECDHE
399 * exchanges) and converts it into a format that the PSA key
400 * agreement API understands.
401 */
Gilles Peskined1959dc2019-12-18 20:44:49 +0100402static inline int mbedtls_psa_tls_ecpoint_to_psa_ec( unsigned char const *src,
Hanno Beckerf75f9122019-01-07 15:36:51 +0000403 size_t srclen,
404 unsigned char *dst,
405 size_t dstlen,
406 size_t *olen )
407{
Hanno Beckerf75f9122019-01-07 15:36:51 +0000408 if( srclen > dstlen )
409 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
410
411 memcpy( dst, src, srclen );
412 *olen = srclen;
413 return( 0 );
414}
415
Hanno Becker87837b22018-11-08 13:32:02 +0000416#endif /* MBEDTLS_USE_PSA_CRYPTO */
417
Gilles Peskinee3ed8022021-02-03 20:04:08 +0100418/* Expose whatever RNG the PSA subsystem uses to applications using the
Gilles Peskine996f2162021-02-16 16:50:00 +0100419 * mbedtls_xxx API. The declarations and definitions here need to be
420 * consistent with the implementation in library/psa_crypto_random_impl.h.
421 * See that file for implementation documentation. */
Gilles Peskinee3ed8022021-02-03 20:04:08 +0100422#if defined(MBEDTLS_PSA_CRYPTO_C)
423
424/* The type of a `f_rng` random generator function that many library functions
425 * take.
426 *
427 * This type name is not part of the Mbed TLS stable API. It may be renamed
428 * or moved without warning.
429 */
430typedef int mbedtls_f_rng_t( void *p_rng, unsigned char *output, size_t output_size );
431
432#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
433
434/** The random generator function for the PSA subsystem.
435 *
436 * This function is suitable as the `f_rng` random generator function
Gilles Peskine2cff7e22021-02-16 16:49:42 +0100437 * parameter of many `mbedtls_xxx` functions. Use #MBEDTLS_PSA_RANDOM_STATE
438 * to obtain the \p p_rng parameter.
Gilles Peskinee3ed8022021-02-03 20:04:08 +0100439 *
440 * The implementation of this function depends on the configuration of the
441 * library.
Gilles Peskine2cff7e22021-02-16 16:49:42 +0100442 *
Gilles Peskinee3ed8022021-02-03 20:04:08 +0100443 * \note Depending on the configuration, this may be a function or
444 * a pointer to a function.
445 *
446 * \note This function may only be used if the PSA crypto subsystem is active.
447 * This means that you must call psa_crypto_init() before any call to
448 * this function, and you must not call this function after calling
449 * mbedtls_psa_crypto_free().
450 *
451 * \param p_rng The random generator context. This must be
452 * #MBEDTLS_PSA_RANDOM_STATE. No other state is
453 * supported.
454 * \param output The buffer to fill. It must have room for
455 * \c output_size bytes.
456 * \param output_size The number of bytes to write to \p output.
457 * This function may fail if \p output_size is too
458 * large. It is guaranteed to accept any output size
459 * requested by Mbed TLS library functions. The
460 * maximum request size depends on the library
461 * configuration.
462 *
463 * \return \c 0 on success.
464 * \return An `MBEDTLS_ERR_ENTROPY_xxx`,
Gilles Peskine2cff7e22021-02-16 16:49:42 +0100465 * `MBEDTLS_ERR_PLATFORM_xxx,
Gilles Peskinee3ed8022021-02-03 20:04:08 +0100466 * `MBEDTLS_ERR_CTR_DRBG_xxx` or
467 * `MBEDTLS_ERR_HMAC_DRBG_xxx` on error.
468 */
469int mbedtls_psa_get_random( void *p_rng,
470 unsigned char *output,
471 size_t output_size );
472
473/** The random generator state for the PSA subsystem.
474 *
475 * This macro expands to an expression which is suitable as the `p_rng`
476 * random generator state parameter of many `mbedtls_xxx` functions.
477 * It must be used in combination with the random generator function
478 * mbedtls_psa_get_random().
479 *
480 * The implementation of this macro depends on the configuration of the
481 * library. Do not make any assumption on its nature.
482 */
483#define MBEDTLS_PSA_RANDOM_STATE NULL
484
485#else /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
486
487#if defined(MBEDTLS_CTR_DRBG_C)
488#include "mbedtls/ctr_drbg.h"
489typedef mbedtls_ctr_drbg_context mbedtls_psa_drbg_context_t;
490static mbedtls_f_rng_t *const mbedtls_psa_get_random = mbedtls_ctr_drbg_random;
491#elif defined(MBEDTLS_HMAC_DRBG_C)
492#include "mbedtls/hmac_drbg.h"
493typedef mbedtls_hmac_drbg_context mbedtls_psa_drbg_context_t;
494static mbedtls_f_rng_t *const mbedtls_psa_get_random = mbedtls_hmac_drbg_random;
495#endif
496extern mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state;
497
498#define MBEDTLS_PSA_RANDOM_STATE mbedtls_psa_random_state
499
500#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
501
502#endif /* MBEDTLS_PSA_CRYPTO_C */
503
Hanno Becker186b65a2018-11-19 15:14:21 +0000504#endif /* MBEDTLS_PSA_UTIL_H */