Add parentheses to unsafe macros

Signed-off-by: Ben Taylor <ben.taylor@linaro.org>

This is a manual backport, not a direct cherry-pick, because the 3.6 branch uses different file paths and keeps a direct PSA_ALG_JPAKE comparison in the PAKE size macros.

(cherry picked from commit ebda5da117af7e57bad47bc3337e364365a8f357)
This commit is contained in:
Ben Taylor 2026-03-16 15:21:02 +00:00
parent 8572c4a99b
commit aacb344f31
7 changed files with 31 additions and 17 deletions

View File

@ -1090,10 +1090,10 @@ typedef uint32_t psa_pake_primitive_t;
* Return 0 if the requested primitive can't be encoded as
* ::psa_pake_primitive_t.
*/
#define PSA_PAKE_PRIMITIVE(pake_type, pake_family, pake_bits) \
((pake_bits & 0xFFFF) != pake_bits) ? 0 : \
((psa_pake_primitive_t) (((pake_type) << 24 | \
(pake_family) << 16) | (pake_bits)))
#define PSA_PAKE_PRIMITIVE(pake_type, pake_family, pake_bits) \
((((pake_bits) & 0xFFFF) != (pake_bits)) ? 0 : \
((psa_pake_primitive_t) ((((pake_type) << 24) | \
((pake_family) << 16)) | (pake_bits))))
/** The key share being sent to or received from the peer.
*
@ -1170,12 +1170,12 @@ typedef uint32_t psa_pake_primitive_t;
* return 0.
*/
#define PSA_PAKE_OUTPUT_SIZE(alg, primitive, output_step) \
(alg == PSA_ALG_JPAKE && \
primitive == PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC, \
((alg) == PSA_ALG_JPAKE && \
(primitive) == PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC, \
PSA_ECC_FAMILY_SECP_R1, 256) ? \
( \
output_step == PSA_PAKE_STEP_KEY_SHARE ? 65 : \
output_step == PSA_PAKE_STEP_ZK_PUBLIC ? 65 : \
(output_step) == PSA_PAKE_STEP_KEY_SHARE ? 65 : \
(output_step) == PSA_PAKE_STEP_ZK_PUBLIC ? 65 : \
32 \
) : \
0)
@ -1200,12 +1200,12 @@ typedef uint32_t psa_pake_primitive_t;
* the parameters are incompatible, return 0.
*/
#define PSA_PAKE_INPUT_SIZE(alg, primitive, input_step) \
(alg == PSA_ALG_JPAKE && \
primitive == PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC, \
((alg) == PSA_ALG_JPAKE && \
(primitive) == PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC, \
PSA_ECC_FAMILY_SECP_R1, 256) ? \
( \
input_step == PSA_PAKE_STEP_KEY_SHARE ? 65 : \
input_step == PSA_PAKE_STEP_ZK_PUBLIC ? 65 : \
(input_step) == PSA_PAKE_STEP_KEY_SHARE ? 65 : \
(input_step) == PSA_PAKE_STEP_ZK_PUBLIC ? 65 : \
32 \
) : \
0)

View File

@ -590,7 +590,7 @@
0))
/** Check if the curve of given family is Weierstrass elliptic curve. */
#define PSA_ECC_FAMILY_IS_WEIERSTRASS(family) ((family & 0xc0) == 0)
#define PSA_ECC_FAMILY_IS_WEIERSTRASS(family) (((family) & 0xc0) == 0)
/** SEC Koblitz curves over prime fields.
*

View File

@ -5012,14 +5012,14 @@ int mbedtls_ecp_mod_p192_raw(mbedtls_mpi_uint *Np, size_t Nn)
#if defined(MBEDTLS_HAVE_INT32) /* 32 bit */
#define MAX32 X_limbs
#define MAX32 (X_limbs)
#define A(j) X[j]
#define STORE32 X[i] = (mbedtls_mpi_uint) cur;
#define STORE0 X[i] = 0;
#else /* 64 bit */
#define MAX32 X_limbs * 2
#define MAX32 ((X_limbs) * 2)
#define A(j) \
(j) % 2 ? \
(uint32_t) (X[(j) / 2] >> 32) : \

View File

@ -153,7 +153,7 @@ static inline mbedtls_ecp_group_id mbedtls_pk_get_ec_group_id(const mbedtls_pk_c
#endif /* MBEDTLS_ECP_HAVE_CURVE25519 || MBEDTLS_ECP_DP_CURVE448 */
#define MBEDTLS_PK_IS_RFC8410_GROUP_ID(id) \
((id == MBEDTLS_ECP_DP_CURVE25519) || (id == MBEDTLS_ECP_DP_CURVE448))
(((id) == MBEDTLS_ECP_DP_CURVE25519) || ((id) == MBEDTLS_ECP_DP_CURVE448))
static inline int mbedtls_pk_is_rfc8410(const mbedtls_pk_context *pk)
{

View File

@ -72,7 +72,7 @@
* YMMV.
*/
/* Helper macro to set the values of the higher bits in unused low positions */
#define H(b63, b31, b15) (b63 << 6 | b31 << 5 | b15 << 4)
#define H(b63, b31, b15) (((b63) << 6) | ((b31) << 5) | ((b15) << 4))
static const uint8_t iota_r_packed[24] = {
H(0, 0, 0) | 0x01, H(0, 0, 1) | 0x82, H(1, 0, 1) | 0x8a, H(1, 1, 1) | 0x00,
H(0, 0, 1) | 0x8b, H(0, 1, 0) | 0x01, H(1, 1, 1) | 0x81, H(1, 0, 1) | 0x09,

View File

@ -11173,6 +11173,7 @@ void ecjpake_size_macros()
{
const psa_algorithm_t alg = PSA_ALG_JPAKE;
const size_t bits = 256;
const psa_pake_primitive_t invalid_primitive = 1;
const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits);
const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
@ -11209,5 +11210,11 @@ void ecjpake_size_macros()
PSA_PAKE_INPUT_MAX_SIZE);
TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
PSA_PAKE_INPUT_MAX_SIZE);
/* Invalid primitives must not match the supported J-PAKE suite. */
TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, invalid_primitive,
PSA_PAKE_STEP_KEY_SHARE), 0);
TEST_EQUAL(PSA_PAKE_INPUT_SIZE(alg, invalid_primitive,
PSA_PAKE_STEP_KEY_SHARE), 0);
}
/* END_CASE */

View File

@ -937,6 +937,7 @@ void ecjpake_size_macros()
{
const psa_algorithm_t alg = PSA_ALG_JPAKE;
const size_t bits = 256;
const psa_pake_primitive_t invalid_primitive = 1;
const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits);
const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
@ -973,6 +974,12 @@ void ecjpake_size_macros()
PSA_PAKE_INPUT_MAX_SIZE);
TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
PSA_PAKE_INPUT_MAX_SIZE);
/* Invalid primitives must not match the supported J-PAKE suite. */
TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, invalid_primitive,
PSA_PAKE_STEP_KEY_SHARE), 0);
TEST_EQUAL(PSA_PAKE_INPUT_SIZE(alg, invalid_primitive,
PSA_PAKE_STEP_KEY_SHARE), 0);
}
/* END_CASE */