Skip to content

Commit 07d8aef

Browse files
committed
mbedtls: Add mbedtls_ecc_group_to_psa()
We'd like to enable Mbed TLS's PK module in using TF-M's PSA implementation, even if it doesn't expose the same set of PSA extensions as Mbed TLS's PSA implementation. To do this, we add mbedtls_ecc_group_to_psa() in its own header available when using the latest TF-M. Add mbedtls_ecc_group_to_psa(), one of Mbed TLS's PSA compatibility helpers, for internal use by the Mbed TLS PK module. Without this conversion function, the Mbed TLS PK module is unable to use any PSA implementation other than one which provides a compatible set of PSA extensions.
1 parent 5434722 commit 07d8aef

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/**
2+
* \file mbedtls_ecc_group_to_psa.h
3+
*
4+
* Excerpted from Mbed TLS for internal use by Mbed TLS's PK module to
5+
* interface with generic PSA Crypto implementations.
6+
*
7+
*/
8+
/*
9+
* Copyright The Mbed TLS Contributors
10+
* SPDX-License-Identifier: Apache-2.0
11+
*
12+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
13+
* not use this file except in compliance with the License.
14+
* You may obtain a copy of the License at
15+
*
16+
* http://www.apache.org/licenses/LICENSE-2.0
17+
*
18+
* Unless required by applicable law or agreed to in writing, software
19+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
20+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21+
* See the License for the specific language governing permissions and
22+
* limitations under the License.
23+
*/
24+
25+
#ifndef MBEDTLS_ECC_GROUP_TO_PSA_H
26+
#define MBEDTLS_ECC_GROUP_TO_PSA_H
27+
28+
//#include "mbedtls/platform_util.h"
29+
30+
//#include "crypto_compat.h"
31+
32+
#ifdef __cplusplus
33+
extern "C" {
34+
#endif
35+
36+
/** \defgroup psa_tls_helpers TLS helper functions
37+
* @{
38+
*/
39+
40+
#if defined(MBEDTLS_ECP_C)
41+
#include <mbedtls/ecp.h>
42+
43+
/** Convert an ECC curve identifier from the Mbed TLS encoding to PSA.
44+
*
45+
* \note This function is provided solely for the convenience of
46+
* Mbed TLS and may be removed at any time without notice.
47+
*
48+
* \param grpid An Mbed TLS elliptic curve identifier
49+
* (`MBEDTLS_ECP_DP_xxx`).
50+
* \param[out] bits On success, the bit size of the curve.
51+
*
52+
* \return The corresponding PSA elliptic curve identifier
53+
* (`PSA_ECC_FAMILY_xxx`).
54+
* \return \c 0 on failure (\p grpid is not recognized).
55+
*/
56+
static inline psa_ecc_family_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid,
57+
size_t *bits )
58+
{
59+
switch( grpid )
60+
{
61+
case MBEDTLS_ECP_DP_SECP192R1:
62+
*bits = 192;
63+
return( PSA_ECC_FAMILY_SECP_R1 );
64+
case MBEDTLS_ECP_DP_SECP224R1:
65+
*bits = 224;
66+
return( PSA_ECC_FAMILY_SECP_R1 );
67+
case MBEDTLS_ECP_DP_SECP256R1:
68+
*bits = 256;
69+
return( PSA_ECC_FAMILY_SECP_R1 );
70+
case MBEDTLS_ECP_DP_SECP384R1:
71+
*bits = 384;
72+
return( PSA_ECC_FAMILY_SECP_R1 );
73+
case MBEDTLS_ECP_DP_SECP521R1:
74+
*bits = 521;
75+
return( PSA_ECC_FAMILY_SECP_R1 );
76+
case MBEDTLS_ECP_DP_BP256R1:
77+
*bits = 256;
78+
return( PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
79+
case MBEDTLS_ECP_DP_BP384R1:
80+
*bits = 384;
81+
return( PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
82+
case MBEDTLS_ECP_DP_BP512R1:
83+
*bits = 512;
84+
return( PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
85+
case MBEDTLS_ECP_DP_CURVE25519:
86+
*bits = 255;
87+
return( PSA_ECC_FAMILY_MONTGOMERY );
88+
case MBEDTLS_ECP_DP_SECP192K1:
89+
*bits = 192;
90+
return( PSA_ECC_FAMILY_SECP_K1 );
91+
case MBEDTLS_ECP_DP_SECP224K1:
92+
*bits = 224;
93+
return( PSA_ECC_FAMILY_SECP_K1 );
94+
case MBEDTLS_ECP_DP_SECP256K1:
95+
*bits = 256;
96+
return( PSA_ECC_FAMILY_SECP_K1 );
97+
case MBEDTLS_ECP_DP_CURVE448:
98+
*bits = 448;
99+
return( PSA_ECC_FAMILY_MONTGOMERY );
100+
default:
101+
*bits = 0;
102+
return( 0 );
103+
}
104+
}
105+
106+
#endif /* MBEDTLS_ECP_C */
107+
108+
/**@}*/
109+
110+
#ifdef __cplusplus
111+
}
112+
#endif
113+
114+
#endif /* MBEDTLS_ECC_GROUP_TO_PSA_H */

0 commit comments

Comments
 (0)