ecp: clarify static constants in grp

This is not really necessary for 1.0+, but for the 3.6 branch where the
structure and these constants are still public, it is cleaner.

Signed-off-by: Manuel Pégourié-Gonnard <[email protected]>
This commit is contained in:
Manuel Pégourié-Gonnard
2026-05-18 10:45:11 +02:00
parent 0ebb3a9ff0
commit 0f8a09a99c
3 changed files with 21 additions and 16 deletions
+1 -1
View File
@@ -248,7 +248,7 @@ typedef struct mbedtls_ecp_group {
private keys. */
/* End of public fields */
unsigned int MBEDTLS_PRIVATE(h); /*!< \internal 1 if the constants are static. */
unsigned int MBEDTLS_PRIVATE(h); /*!< \internal 1 if all the constants are static, 2 if only N and P are static */
int(*MBEDTLS_PRIVATE(modp))(mbedtls_mpi *); /*!< The function for fast pseudo-reduction
mod \p P (see above).*/
int(*MBEDTLS_PRIVATE(t_pre))(mbedtls_ecp_point *, void *); /*!< Unused. */
+5 -1
View File
@@ -582,7 +582,11 @@ void mbedtls_ecp_group_free(mbedtls_ecp_group *grp)
mbedtls_mpi_free(&grp->A);
mbedtls_mpi_free(&grp->B);
mbedtls_ecp_point_free(&grp->G);
/* Don't free N and P, those are static */
if (grp->h != 2) {
mbedtls_mpi_free(&grp->N);
mbedtls_mpi_free(&grp->P);
}
}
if (!ecp_group_is_static_comb_table(grp) && grp->T != NULL) {
+15 -14
View File
@@ -4542,7 +4542,7 @@ static int ecp_group_load(mbedtls_ecp_group *grp,
grp->pbits = mbedtls_mpi_bitlen(&grp->P);
grp->nbits = mbedtls_mpi_bitlen(&grp->N);
grp->h = 1;
grp->h = 1; /* All constants static */
grp->T = (mbedtls_ecp_point *) T;
/*
@@ -4663,14 +4663,17 @@ static int ecp_use_curve25519(mbedtls_ecp_group *grp)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
/* Actually ( A + 2 ) / 4 */
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&grp->A, curve25519_a24));
/* Set this before anything that can fail and call ecp_group_free(). */
grp->h = 2; /* N and P static, but not A, B or G */
ecp_mpi_load(&grp->P, curve25519_p, sizeof(curve25519_p));
grp->pbits = mbedtls_mpi_bitlen(&grp->P);
ecp_mpi_load(&grp->N, curve25519_n, sizeof(curve25519_n));
grp->nbits = 254; /* Actually, the required msb for private keys */
/* Actually ( A + 2 ) / 4 */
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&grp->A, curve25519_a24));
/* Y intentionally not set, since we use x/z coordinates.
* This is used as a marker to identify Montgomery curves! */
@@ -4678,9 +4681,6 @@ static int ecp_use_curve25519(mbedtls_ecp_group *grp)
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&grp->G.Z, 1));
mbedtls_mpi_free(&grp->G.Y);
/* Actually, the required msb for private keys */
grp->nbits = 254;
cleanup:
if (ret != 0) {
mbedtls_ecp_group_free(grp);
@@ -4725,23 +4725,24 @@ static int ecp_use_curve448(mbedtls_ecp_group *grp)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
/* Actually ( A + 2 ) / 4 */
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&grp->A, curve448_a24));
/* Set this before anything that can fail and call ecp_group_free(). */
grp->h = 2; /* N and P static, but not A, B or G */
ecp_mpi_load(&grp->P, curve448_p, sizeof(curve448_p));
grp->pbits = mbedtls_mpi_bitlen(&grp->P);
ecp_mpi_load(&grp->N, curve448_n, sizeof(curve448_n));
grp->nbits = 447; /* Actually, the required msb for private keys */
/* Actually ( A + 2 ) / 4 */
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&grp->A, curve448_a24));
/* Y intentionally not set, since we use x/z coordinates.
* This is used as a marker to identify Montgomery curves! */
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&grp->G.X, 5));
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&grp->G.Z, 1));
mbedtls_mpi_free(&grp->G.Y);
ecp_mpi_load(&grp->N, curve448_n, sizeof(curve448_n));
/* Actually, the required msb for private keys */
grp->nbits = 447;
cleanup:
if (ret != 0) {
mbedtls_ecp_group_free(grp);