Skip to content

Commit 62c8379

Browse files
LDong-Armevedon
authored andcommitted
TARGET_TFM_V1_0: compatibility with Mbed TLS 2.24.0
Mbed TLS 2.24.0 requires a few new macros and an inline function in the PSA Crypto header. This PR adds them to make sure the TF-M v1.0 target (specifically CYTFM_064B0S2_4343W) continues to compile with the new Mbed TLS. Note: Support for older versions of TF-M than v1.2 will be dropped, so existing TF-M targets should migrate to TF-M v1.2 as soon as possible.
1 parent 6ab4b1c commit 62c8379

File tree

3 files changed

+175
-0
lines changed

3 files changed

+175
-0
lines changed

platform/FEATURE_EXPERIMENTAL_API/FEATURE_PSA/TARGET_TFM/TARGET_TFM_V1_0/include/psa/crypto_extra.h

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,91 @@ static inline void psa_set_key_enrollment_algorithm(
5050
attributes->core.policy.alg2 = alg2;
5151
}
5252

53+
#if defined(MBEDTLS_ECP_C)
54+
#include <mbedtls/ecp.h>
55+
56+
/** Convert an ECC curve identifier from the Mbed TLS encoding to PSA.
57+
*
58+
* \note This function is provided solely for the convenience of
59+
* Mbed TLS and may be removed at any time without notice.
60+
*
61+
* \param grpid An Mbed TLS elliptic curve identifier
62+
* (`MBEDTLS_ECP_DP_xxx`).
63+
* \param[out] bits On success, the bit size of the curve.
64+
*
65+
* \return The corresponding PSA elliptic curve identifier
66+
* (`PSA_ECC_FAMILY_xxx`).
67+
* \return \c 0 on failure (\p grpid is not recognized).
68+
*/
69+
static inline psa_ecc_family_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid,
70+
size_t *bits )
71+
{
72+
switch( grpid )
73+
{
74+
case MBEDTLS_ECP_DP_SECP192R1:
75+
*bits = 192;
76+
return( PSA_ECC_FAMILY_SECP_R1 );
77+
case MBEDTLS_ECP_DP_SECP224R1:
78+
*bits = 224;
79+
return( PSA_ECC_FAMILY_SECP_R1 );
80+
case MBEDTLS_ECP_DP_SECP256R1:
81+
*bits = 256;
82+
return( PSA_ECC_FAMILY_SECP_R1 );
83+
case MBEDTLS_ECP_DP_SECP384R1:
84+
*bits = 384;
85+
return( PSA_ECC_FAMILY_SECP_R1 );
86+
case MBEDTLS_ECP_DP_SECP521R1:
87+
*bits = 521;
88+
return( PSA_ECC_FAMILY_SECP_R1 );
89+
case MBEDTLS_ECP_DP_BP256R1:
90+
*bits = 256;
91+
return( PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
92+
case MBEDTLS_ECP_DP_BP384R1:
93+
*bits = 384;
94+
return( PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
95+
case MBEDTLS_ECP_DP_BP512R1:
96+
*bits = 512;
97+
return( PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
98+
case MBEDTLS_ECP_DP_CURVE25519:
99+
*bits = 255;
100+
return( PSA_ECC_FAMILY_MONTGOMERY );
101+
case MBEDTLS_ECP_DP_SECP192K1:
102+
*bits = 192;
103+
return( PSA_ECC_FAMILY_SECP_K1 );
104+
case MBEDTLS_ECP_DP_SECP224K1:
105+
*bits = 224;
106+
return( PSA_ECC_FAMILY_SECP_K1 );
107+
case MBEDTLS_ECP_DP_SECP256K1:
108+
*bits = 256;
109+
return( PSA_ECC_FAMILY_SECP_K1 );
110+
case MBEDTLS_ECP_DP_CURVE448:
111+
*bits = 448;
112+
return( PSA_ECC_FAMILY_MONTGOMERY );
113+
default:
114+
*bits = 0;
115+
return( 0 );
116+
}
117+
}
118+
119+
/** Convert an ECC curve identifier from the PSA encoding to Mbed TLS.
120+
*
121+
* \note This function is provided solely for the convenience of
122+
* Mbed TLS and may be removed at any time without notice.
123+
*
124+
* \param curve A PSA elliptic curve identifier
125+
* (`PSA_ECC_FAMILY_xxx`).
126+
* \param byte_length The byte-length of a private key on \p curve.
127+
*
128+
* \return The corresponding Mbed TLS elliptic curve identifier
129+
* (`MBEDTLS_ECP_DP_xxx`).
130+
* \return #MBEDTLS_ECP_DP_NONE if \c curve is not recognized.
131+
* \return #MBEDTLS_ECP_DP_NONE if \p byte_length is not
132+
* correct for \p curve.
133+
*/
134+
//mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
135+
// size_t byte_length );
136+
#endif /* MBEDTLS_ECP_C */
137+
53138
#ifdef __cplusplus
54139
}
55140
#endif

platform/FEATURE_EXPERIMENTAL_API/FEATURE_PSA/TARGET_TFM/TARGET_TFM_V1_0/include/psa/crypto_types.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ typedef int32_t psa_status_t;
5353
*/
5454
typedef uint32_t psa_key_type_t;
5555

56+
/** The type of PSA elliptic curve family identifiers.
57+
*
58+
* The curve identifier is required to create an ECC key using the
59+
* PSA_KEY_TYPE_ECC_KEY_PAIR() or PSA_KEY_TYPE_ECC_PUBLIC_KEY()
60+
* macros.
61+
*
62+
* Values defined by this standard will never be in the range 0x80-0xff.
63+
* Vendors who define additional families must use an encoding in this range.
64+
*/
65+
typedef uint8_t psa_ecc_family_t;
66+
5667
/** The type of PSA elliptic curve identifiers.
5768
*
5869
* The curve identifier is required to create an ECC key using the

platform/FEATURE_EXPERIMENTAL_API/FEATURE_PSA/TARGET_TFM/TARGET_TFM_V1_0/include/psa/crypto_values.h

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1696,6 +1696,85 @@
16961696
*/
16971697
#define PSA_KEY_DERIVATION_INPUT_SEED ((psa_key_derivation_step_t)0x0204)
16981698

1699+
/** Extract the curve from an elliptic curve key type. */
1700+
#define PSA_KEY_TYPE_ECC_GET_FAMILY(type) \
1701+
((psa_ecc_family_t) (PSA_KEY_TYPE_IS_ECC(type) ? \
1702+
((type) & PSA_KEY_TYPE_ECC_CURVE_MASK) : \
1703+
0))
1704+
1705+
1706+
/** SEC Koblitz curves over prime fields.
1707+
*
1708+
* This family comprises the following curves:
1709+
* secp192k1, secp224k1, secp256k1.
1710+
* They are defined in _Standards for Efficient Cryptography_,
1711+
* _SEC 2: Recommended Elliptic Curve Domain Parameters_.
1712+
* https://www.secg.org/sec2-v2.pdf
1713+
*/
1714+
#define PSA_ECC_FAMILY_SECP_K1 ((psa_ecc_family_t) 0x17)
1715+
1716+
/** SEC random curves over prime fields.
1717+
*
1718+
* This family comprises the following curves:
1719+
* secp192k1, secp224r1, secp256r1, secp384r1, secp521r1.
1720+
* They are defined in _Standards for Efficient Cryptography_,
1721+
* _SEC 2: Recommended Elliptic Curve Domain Parameters_.
1722+
* https://www.secg.org/sec2-v2.pdf
1723+
*/
1724+
#define PSA_ECC_FAMILY_SECP_R1 ((psa_ecc_family_t) 0x12)
1725+
/* SECP160R2 (SEC2 v1, obsolete) */
1726+
#define PSA_ECC_FAMILY_SECP_R2 ((psa_ecc_family_t) 0x1b)
1727+
1728+
/** SEC Koblitz curves over binary fields.
1729+
*
1730+
* This family comprises the following curves:
1731+
* sect163k1, sect233k1, sect239k1, sect283k1, sect409k1, sect571k1.
1732+
* They are defined in _Standards for Efficient Cryptography_,
1733+
* _SEC 2: Recommended Elliptic Curve Domain Parameters_.
1734+
* https://www.secg.org/sec2-v2.pdf
1735+
*/
1736+
#define PSA_ECC_FAMILY_SECT_K1 ((psa_ecc_family_t) 0x27)
1737+
1738+
/** SEC random curves over binary fields.
1739+
*
1740+
* This family comprises the following curves:
1741+
* sect163r1, sect233r1, sect283r1, sect409r1, sect571r1.
1742+
* They are defined in _Standards for Efficient Cryptography_,
1743+
* _SEC 2: Recommended Elliptic Curve Domain Parameters_.
1744+
* https://www.secg.org/sec2-v2.pdf
1745+
*/
1746+
#define PSA_ECC_FAMILY_SECT_R1 ((psa_ecc_family_t) 0x22)
1747+
1748+
/** SEC additional random curves over binary fields.
1749+
*
1750+
* This family comprises the following curve:
1751+
* sect163r2.
1752+
* It is defined in _Standards for Efficient Cryptography_,
1753+
* _SEC 2: Recommended Elliptic Curve Domain Parameters_.
1754+
* https://www.secg.org/sec2-v2.pdf
1755+
*/
16991756
/**@}*/
1757+
#define PSA_ECC_FAMILY_SECT_R2 ((psa_ecc_family_t) 0x2b)
1758+
1759+
/** Brainpool P random curves.
1760+
*
1761+
* This family comprises the following curves:
1762+
* brainpoolP160r1, brainpoolP192r1, brainpoolP224r1, brainpoolP256r1,
1763+
* brainpoolP320r1, brainpoolP384r1, brainpoolP512r1.
1764+
* It is defined in RFC 5639.
1765+
*/
1766+
#define PSA_ECC_FAMILY_BRAINPOOL_P_R1 ((psa_ecc_family_t) 0x30)
1767+
1768+
/** Curve25519 and Curve448.
1769+
*
1770+
* This family comprises the following Montgomery curves:
1771+
* - 255-bit: Bernstein et al.,
1772+
* _Curve25519: new Diffie-Hellman speed records_, LNCS 3958, 2006.
1773+
* The algorithm #PSA_ALG_ECDH performs X25519 when used with this curve.
1774+
* - 448-bit: Hamburg,
1775+
* _Ed448-Goldilocks, a new elliptic curve_, NIST ECC Workshop, 2015.
1776+
* The algorithm #PSA_ALG_ECDH performs X448 when used with this curve.
1777+
*/
1778+
#define PSA_ECC_FAMILY_MONTGOMERY ((psa_ecc_family_t) 0x41)
17001779

17011780
#endif /* PSA_CRYPTO_VALUES_H */

0 commit comments

Comments
 (0)