Skip to content

Commit

Permalink
Improve and fix compile initializers for ECDH/ECP
Browse files Browse the repository at this point in the history
Signed-off-by: Waleed Elmelegy <[email protected]>
  • Loading branch information
waleed-elmelegy-arm committed Nov 6, 2024
1 parent a4d0fd1 commit 7817da0
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 26 deletions.
2 changes: 1 addition & 1 deletion tf-psa-crypto/drivers/builtin/include/mbedtls/bignum.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
54 changes: 32 additions & 22 deletions tf-psa-crypto/drivers/builtin/include/mbedtls/ecdh.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand Down Expand Up @@ -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 */
Expand Down
2 changes: 1 addition & 1 deletion tf-psa-crypto/drivers/everest/include/everest/everest.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tf-psa-crypto/drivers/everest/include/everest/x25519.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion tf-psa-crypto/include/psa/crypto_builtin_composites.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7817da0

Please sign in to comment.