From 7817da0833c4d9b22c21f24af6531b3d8377c4ef Mon Sep 17 00:00:00 2001 From: Waleed Elmelegy Date: Wed, 6 Nov 2024 13:52:34 +0000 Subject: [PATCH] Improve and fix compile initializers for ECDH/ECP Signed-off-by: Waleed Elmelegy --- .../drivers/builtin/include/mbedtls/bignum.h | 2 +- .../drivers/builtin/include/mbedtls/ecdh.h | 54 +++++++++++-------- .../drivers/everest/include/everest/everest.h | 2 +- .../drivers/everest/include/everest/x25519.h | 2 +- .../include/psa/crypto_builtin_composites.h | 2 +- 5 files changed, 36 insertions(+), 26 deletions(-) diff --git a/tf-psa-crypto/drivers/builtin/include/mbedtls/bignum.h b/tf-psa-crypto/drivers/builtin/include/mbedtls/bignum.h index 22d5d8484016..40b7277d1bd6 100644 --- a/tf-psa-crypto/drivers/builtin/include/mbedtls/bignum.h +++ b/tf-psa-crypto/drivers/builtin/include/mbedtls/bignum.h @@ -238,7 +238,7 @@ typedef struct mbedtls_mpi { } mbedtls_mpi; -#define MBEDTLS_MPI_INIT { 0, 0, 0 } +#define MBEDTLS_MPI_INIT { 0, 1, 0 } /** * \brief Initialize an MPI context. diff --git a/tf-psa-crypto/drivers/builtin/include/mbedtls/ecdh.h b/tf-psa-crypto/drivers/builtin/include/mbedtls/ecdh.h index c85301ad487f..99f0375b3ad1 100644 --- a/tf-psa-crypto/drivers/builtin/include/mbedtls/ecdh.h +++ b/tf-psa-crypto/drivers/builtin/include/mbedtls/ecdh.h @@ -96,14 +96,14 @@ typedef struct mbedtls_ecdh_context_mbed { #endif #if defined(MBEDTLS_ECP_RESTARTABLE) -#define MBEDTLS_ECDH_CTX_MBED_INIT { MBEDTLS_ECP_GROUP_INIT, MBEDTLS_MPI_INIT, \ - MBEDTLS_ECP_POINT_INIT, \ - MBEDTLS_ECP_POINT_INIT, MBEDTLS_MPI_INIT, \ - MBEDTLS_ECP_RESTART_INIT } +#define MBEDTLS_ECDH_CONTEXT_MBED_INIT { MBEDTLS_ECP_GROUP_INIT, MBEDTLS_MPI_INIT, \ + MBEDTLS_ECP_POINT_INIT, \ + MBEDTLS_ECP_POINT_INIT, MBEDTLS_MPI_INIT, \ + MBEDTLS_ECP_RESTART_INIT } #else -#define MBEDTLS_ECDH_CTX_MBED_INIT { MBEDTLS_ECP_GROUP_INIT, MBEDTLS_MPI_INIT, \ - MBEDTLS_ECP_POINT_INIT, \ - MBEDTLS_ECP_POINT_INIT, MBEDTLS_MPI_INIT } +#define MBEDTLS_ECDH_CONTEXT_MBED_INIT { MBEDTLS_ECP_GROUP_INIT, MBEDTLS_MPI_INIT, \ + MBEDTLS_ECP_POINT_INIT, \ + MBEDTLS_ECP_POINT_INIT, MBEDTLS_MPI_INIT } #endif /** @@ -154,31 +154,41 @@ mbedtls_ecdh_context; #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) #if defined(MBEDTLS_ECP_RESTARTABLE) -#define MBEDTLS_ECDH_CTX_INIT { MBEDTLS_ECP_GROUP_INIT, MBEDTLS_MPI_INIT, MBEDTLS_ECP_POINT_INIT, \ - MBEDTLS_ECP_POINT_INIT, MBEDTLS_MPI_INIT, 0, MBEDTLS_ECP_POINT_INIT, \ - MBEDTLS_ECP_POINT_INIT, MBEDTLS_MPI_INIT, 0, \ - MBEDTLS_ECP_RESTART_INIT } +#define MBEDTLS_ECDH_CONTEXT_INIT { MBEDTLS_ECP_GROUP_INIT, MBEDTLS_MPI_INIT, \ + MBEDTLS_ECP_POINT_INIT, \ + MBEDTLS_ECP_POINT_INIT, MBEDTLS_MPI_INIT, \ + MBEDTLS_ECP_PF_UNCOMPRESSED, \ + MBEDTLS_ECP_POINT_INIT, MBEDTLS_ECP_POINT_INIT, \ + MBEDTLS_MPI_INIT, 0, \ + MBEDTLS_ECP_RESTART_INIT } #else -#define MBEDTLS_ECDH_CTX_INIT { MBEDTLS_ECP_GROUP_INIT, MBEDTLS_MPI_INIT, MBEDTLS_ECP_POINT_INIT, \ - MBEDTLS_ECP_POINT_INIT, MBEDTLS_MPI_INIT, 0, MBEDTLS_ECP_POINT_INIT, \ - MBEDTLS_ECP_POINT_INIT, MBEDTLS_MPI_INIT } +#define MBEDTLS_ECDH_CONTEXT_INIT { MBEDTLS_ECP_GROUP_INIT, MBEDTLS_MPI_INIT, \ + MBEDTLS_ECP_POINT_INIT, \ + MBEDTLS_ECP_POINT_INIT, MBEDTLS_MPI_INIT, \ + MBEDTLS_ECP_PF_UNCOMPRESSED, \ + MBEDTLS_ECP_POINT_INIT, MBEDTLS_ECP_POINT_INIT, \ + MBEDTLS_MPI_INIT } #endif /* MBEDTLS_ECP_RESTARTABLE */ #else #if !defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED) #if defined(MBEDTLS_ECP_RESTARTABLE) -#define MBEDTLS_ECDH_CTX_INIT { 0, MBEDTLS_ECP_DP_NONE, MBEDTLS_ECDH_VARIANT_NONE, \ - MBEDTLS_ECDH_CTX_MBED_INIT, 0 } +#define MBEDTLS_ECDH_CONTEXT_INIT { MBEDTLS_ECP_PF_UNCOMPRESSED, MBEDTLS_ECP_DP_NONE, \ + MBEDTLS_ECDH_VARIANT_NONE, \ + { .mbed_ecdh = MBEDTLS_ECDH_CONTEXT_MBED_INIT }, 0 } #else -#define MBEDTLS_ECDH_CTX_INIT { 0, MBEDTLS_ECP_DP_NONE, MBEDTLS_ECDH_VARIANT_NONE, \ - MBEDTLS_ECDH_CTX_MBED_INIT } +#define MBEDTLS_ECDH_CONTEXT_INIT { MBEDTLS_ECP_PF_UNCOMPRESSED, MBEDTLS_ECP_DP_NONE, \ + MBEDTLS_ECDH_VARIANT_NONE, \ + { .mbed_ecdh = MBEDTLS_ECDH_CONTEXT_MBED_INIT } } #endif /* MBEDTLS_ECP_RESTARTABLE */ #else #if defined(MBEDTLS_ECP_RESTARTABLE) -#define MBEDTLS_ECDH_CTX_INIT { 0, MBEDTLS_ECP_DP_NONE, MBEDTLS_ECDH_VARIANT_NONE, \ - MBEDTLS_ECDH_CTX_EVEREST_INIT, 0 } +#define MBEDTLS_ECDH_CONTEXT_INIT { MBEDTLS_ECP_PF_UNCOMPRESSED, MBEDTLS_ECP_DP_NONE, \ + MBEDTLS_ECDH_VARIANT_NONE, \ + { .everest_ecdh = MBEDTLS_ECDH_CONTEXT_EVEREST_INIT }, 0 } #else -#define MBEDTLS_ECDH_CTX_INIT { 0, MBEDTLS_ECP_DP_NONE, MBEDTLS_ECDH_VARIANT_NONE, \ - MBEDTLS_ECDH_CTX_EVEREST_INIT } +#define MBEDTLS_ECDH_CONTEXT_INIT { MBEDTLS_ECP_PF_UNCOMPRESSED, MBEDTLS_ECP_DP_NONE, \ + MBEDTLS_ECDH_VARIANT_NONE, \ + { .everest_ecdh = MBEDTLS_ECDH_CONTEXT_EVEREST_INIT } } #endif /* MBEDTLS_ECP_RESTARTABLE */ #endif /* !MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED */ #endif /* MBEDTLS_ECDH_LEGACY_CONTEXT */ diff --git a/tf-psa-crypto/drivers/everest/include/everest/everest.h b/tf-psa-crypto/drivers/everest/include/everest/everest.h index 2709344398e8..f83413e0247c 100644 --- a/tf-psa-crypto/drivers/everest/include/everest/everest.h +++ b/tf-psa-crypto/drivers/everest/include/everest/everest.h @@ -41,7 +41,7 @@ typedef struct { mbedtls_x25519_context ctx; } mbedtls_ecdh_context_everest; -#define MBEDTLS_ECDH_CTX_EVEREST_INIT {MBEDTLS_X25519_CTX_INIT} +#define MBEDTLS_ECDH_CONTEXT_EVEREST_INIT {MBEDTLS_X25519_CONTEXT_INIT} /** * \brief This function sets up the ECDH context with the information diff --git a/tf-psa-crypto/drivers/everest/include/everest/x25519.h b/tf-psa-crypto/drivers/everest/include/everest/x25519.h index aa71388f5607..39d01dee0d8a 100644 --- a/tf-psa-crypto/drivers/everest/include/everest/x25519.h +++ b/tf-psa-crypto/drivers/everest/include/everest/x25519.h @@ -47,7 +47,7 @@ typedef struct unsigned char peer_point[MBEDTLS_X25519_KEY_SIZE_BYTES]; } mbedtls_x25519_context; -#define MBEDTLS_X25519_CTX_INIT {{0}, {0}} +#define MBEDTLS_X25519_CONTEXT_INIT {{0}, {0}} /** * \brief This function initializes an x25519 context. diff --git a/tf-psa-crypto/include/psa/crypto_builtin_composites.h b/tf-psa-crypto/include/psa/crypto_builtin_composites.h index 706a1055ab64..14e759b13d86 100644 --- a/tf-psa-crypto/include/psa/crypto_builtin_composites.h +++ b/tf-psa-crypto/include/psa/crypto_builtin_composites.h @@ -243,7 +243,7 @@ typedef struct { } mbedtls_psa_key_agreement_interruptible_operation_t; #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) && defined(MBEDTLS_ECP_RESTARTABLE) -#define MBEDTLS_PSA_KEY_AGREEMENT_IOP_INIT { MBEDTLS_ECDH_CTX_INIT, 0 } +#define MBEDTLS_PSA_KEY_AGREEMENT_IOP_INIT { MBEDTLS_ECDH_CONTEXT_INIT, 0 } #else #define MBEDTLS_PSA_KEY_AGREEMENT_IOP_INIT { 0 } #endif