From 09a455e21a0f9c13e74626d557a3686838b52890 Mon Sep 17 00:00:00 2001 From: Agathiyan Bragadeesh Date: Wed, 12 Jul 2023 11:11:42 +0100 Subject: [PATCH 1/7] Add macros for mpi uint max sizes Signed-off-by: Agathiyan Bragadeesh --- include/mbedtls/bignum.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/include/mbedtls/bignum.h b/include/mbedtls/bignum.h index e7f3131740..83a91181aa 100644 --- a/include/mbedtls/bignum.h +++ b/include/mbedtls/bignum.h @@ -129,6 +129,7 @@ #endif /* !MBEDTLS_HAVE_INT64 */ typedef int64_t mbedtls_mpi_sint; typedef uint64_t mbedtls_mpi_uint; +#define MBEDTLS_MPI_UINT_MAX 18446744073709551615UL #elif defined(__GNUC__) && ( \ defined(__amd64__) || defined(__x86_64__) || \ defined(__ppc64__) || defined(__powerpc64__) || \ @@ -141,6 +142,7 @@ typedef uint64_t mbedtls_mpi_uint; #endif /* MBEDTLS_HAVE_INT64 */ typedef int64_t mbedtls_mpi_sint; typedef uint64_t mbedtls_mpi_uint; +#define MBEDTLS_MPI_UINT_MAX 18446744073709551615UL #if !defined(MBEDTLS_NO_UDBL_DIVISION) /* mbedtls_t_udbl defined as 128-bit unsigned int */ typedef unsigned int mbedtls_t_udbl __attribute__((mode(TI))); @@ -156,6 +158,7 @@ typedef unsigned int mbedtls_t_udbl __attribute__((mode(TI))); #endif /* !MBEDTLS_HAVE_INT64 */ typedef int64_t mbedtls_mpi_sint; typedef uint64_t mbedtls_mpi_uint; +#define MBEDTLS_MPI_UINT_MAX 18446744073709551615UL #if !defined(MBEDTLS_NO_UDBL_DIVISION) /* mbedtls_t_udbl defined as 128-bit unsigned int */ typedef __uint128_t mbedtls_t_udbl; @@ -165,6 +168,7 @@ typedef __uint128_t mbedtls_t_udbl; /* Force 64-bit integers with unknown compiler */ typedef int64_t mbedtls_mpi_sint; typedef uint64_t mbedtls_mpi_uint; +#define MBEDTLS_MPI_UINT_MAX 18446744073709551615UL #endif #endif /* !MBEDTLS_HAVE_INT32 */ @@ -175,6 +179,7 @@ typedef uint64_t mbedtls_mpi_uint; #endif /* !MBEDTLS_HAVE_INT32 */ typedef int32_t mbedtls_mpi_sint; typedef uint32_t mbedtls_mpi_uint; +#define MBEDTLS_MPI_UINT_MAX 4294967295UL #if !defined(MBEDTLS_NO_UDBL_DIVISION) typedef uint64_t mbedtls_t_udbl; #define MBEDTLS_HAVE_UDBL From 271a95331e0383ed7b28cae4191d1077fa13bf1a Mon Sep 17 00:00:00 2001 From: Agathiyan Bragadeesh Date: Wed, 12 Jul 2023 11:15:17 +0100 Subject: [PATCH 2/7] Remove tautology in mbedtls_mpi_core_clz Signed-off-by: Agathiyan Bragadeesh --- library/bignum_core.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/library/bignum_core.c b/library/bignum_core.c index de57cfc04c..fd59ba1e1e 100644 --- a/library/bignum_core.c +++ b/library/bignum_core.c @@ -35,23 +35,22 @@ size_t mbedtls_mpi_core_clz(mbedtls_mpi_uint a) { + #if defined(__has_builtin) +#if (MBEDTLS_MPI_UINT_MAX == UINT_MAX) #if __has_builtin(__builtin_clz) - if (sizeof(mbedtls_mpi_uint) == sizeof(unsigned int)) { - return (size_t) __builtin_clz(a); - } + return (size_t) __builtin_clz(a); #endif +#elif (MBEDTLS_MPI_UINT_MAX == ULONG_MAX) #if __has_builtin(__builtin_clzl) - if (sizeof(mbedtls_mpi_uint) == sizeof(unsigned long)) { - return (size_t) __builtin_clzl(a); - } + return (size_t) __builtin_clzl(a); #endif +#elif (MBEDTLS_MPI_UINT_MAX == ULLONG_MAX) #if __has_builtin(__builtin_clzll) - if (sizeof(mbedtls_mpi_uint) == sizeof(unsigned long long)) { - return (size_t) __builtin_clzll(a); - } + return (size_t) __builtin_clzll(a); #endif #endif +#else size_t j; mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1); @@ -64,6 +63,7 @@ size_t mbedtls_mpi_core_clz(mbedtls_mpi_uint a) } return j; +#endif } size_t mbedtls_mpi_core_bitlen(const mbedtls_mpi_uint *A, size_t A_limbs) From e55a1e1cf47ffc068d740f3f91ea254725271fd0 Mon Sep 17 00:00:00 2001 From: Agathiyan Bragadeesh Date: Mon, 17 Jul 2023 15:00:19 +0100 Subject: [PATCH 3/7] Refactor preprocessing for arm none Signed-off-by: Agathiyan Bragadeesh --- library/bignum_core.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/library/bignum_core.c b/library/bignum_core.c index fd59ba1e1e..81a5acabf2 100644 --- a/library/bignum_core.c +++ b/library/bignum_core.c @@ -35,21 +35,18 @@ size_t mbedtls_mpi_core_clz(mbedtls_mpi_uint a) { - + #if defined(__has_builtin) -#if (MBEDTLS_MPI_UINT_MAX == UINT_MAX) -#if __has_builtin(__builtin_clz) - return (size_t) __builtin_clz(a); -#endif -#elif (MBEDTLS_MPI_UINT_MAX == ULONG_MAX) -#if __has_builtin(__builtin_clzl) - return (size_t) __builtin_clzl(a); -#endif -#elif (MBEDTLS_MPI_UINT_MAX == ULLONG_MAX) -#if __has_builtin(__builtin_clzll) - return (size_t) __builtin_clzll(a); +#if (MBEDTLS_MPI_UINT_MAX == UINT_MAX) && __has_builtin(__builtin_clz) + #define core_clz __builtin_clz +#elif (MBEDTLS_MPI_UINT_MAX == ULONG_MAX) && __has_builtin(__builtin_clzl) + #define core_clz __builtin_clzl +#elif (MBEDTLS_MPI_UINT_MAX == ULLONG_MAX) && __has_builtin(__builtin_clzll) + #define core_clz __builtin_clzll #endif #endif +#if defined(core_clz) + return (size_t) core_clz(a); #else size_t j; mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1); From 5058a5b5adf92cb0c660483ed6f057b478166953 Mon Sep 17 00:00:00 2001 From: Agathiyan Bragadeesh Date: Mon, 17 Jul 2023 15:23:52 +0100 Subject: [PATCH 4/7] Remove trailing whitespace bignum_core Signed-off-by: Agathiyan Bragadeesh --- library/bignum_core.c | 1 - 1 file changed, 1 deletion(-) diff --git a/library/bignum_core.c b/library/bignum_core.c index 81a5acabf2..8bf819ce6a 100644 --- a/library/bignum_core.c +++ b/library/bignum_core.c @@ -35,7 +35,6 @@ size_t mbedtls_mpi_core_clz(mbedtls_mpi_uint a) { - #if defined(__has_builtin) #if (MBEDTLS_MPI_UINT_MAX == UINT_MAX) && __has_builtin(__builtin_clz) #define core_clz __builtin_clz From 900e20d3a2bf09933e456d463ba9eaa5c77d606d Mon Sep 17 00:00:00 2001 From: Agathiyan Bragadeesh <48658345+AgathiyanB@users.noreply.github.com> Date: Mon, 17 Jul 2023 16:27:21 +0100 Subject: [PATCH 5/7] Change MBEDTLS_MPI_UINT_MAX suffix Co-authored-by: Gilles Peskine Signed-off-by: Agathiyan Bragadeesh <48658345+AgathiyanB@users.noreply.github.com> --- include/mbedtls/bignum.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/mbedtls/bignum.h b/include/mbedtls/bignum.h index 83a91181aa..a0df6089cb 100644 --- a/include/mbedtls/bignum.h +++ b/include/mbedtls/bignum.h @@ -129,7 +129,7 @@ #endif /* !MBEDTLS_HAVE_INT64 */ typedef int64_t mbedtls_mpi_sint; typedef uint64_t mbedtls_mpi_uint; -#define MBEDTLS_MPI_UINT_MAX 18446744073709551615UL +#define MBEDTLS_MPI_UINT_MAX 18446744073709551615U #elif defined(__GNUC__) && ( \ defined(__amd64__) || defined(__x86_64__) || \ defined(__ppc64__) || defined(__powerpc64__) || \ From 197565062ad8577b7a02e3a4ee29a6ec33cc103e Mon Sep 17 00:00:00 2001 From: Agathiyan Bragadeesh Date: Mon, 17 Jul 2023 16:43:19 +0100 Subject: [PATCH 6/7] Make consistent suffix MBEDTLS_MPI_UINT_MAX Signed-off-by: Agathiyan Bragadeesh --- include/mbedtls/bignum.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/mbedtls/bignum.h b/include/mbedtls/bignum.h index a0df6089cb..6fb79cf2bf 100644 --- a/include/mbedtls/bignum.h +++ b/include/mbedtls/bignum.h @@ -142,7 +142,7 @@ typedef uint64_t mbedtls_mpi_uint; #endif /* MBEDTLS_HAVE_INT64 */ typedef int64_t mbedtls_mpi_sint; typedef uint64_t mbedtls_mpi_uint; -#define MBEDTLS_MPI_UINT_MAX 18446744073709551615UL +#define MBEDTLS_MPI_UINT_MAX 18446744073709551615U #if !defined(MBEDTLS_NO_UDBL_DIVISION) /* mbedtls_t_udbl defined as 128-bit unsigned int */ typedef unsigned int mbedtls_t_udbl __attribute__((mode(TI))); @@ -158,7 +158,7 @@ typedef unsigned int mbedtls_t_udbl __attribute__((mode(TI))); #endif /* !MBEDTLS_HAVE_INT64 */ typedef int64_t mbedtls_mpi_sint; typedef uint64_t mbedtls_mpi_uint; -#define MBEDTLS_MPI_UINT_MAX 18446744073709551615UL +#define MBEDTLS_MPI_UINT_MAX 18446744073709551615U #if !defined(MBEDTLS_NO_UDBL_DIVISION) /* mbedtls_t_udbl defined as 128-bit unsigned int */ typedef __uint128_t mbedtls_t_udbl; @@ -168,7 +168,7 @@ typedef __uint128_t mbedtls_t_udbl; /* Force 64-bit integers with unknown compiler */ typedef int64_t mbedtls_mpi_sint; typedef uint64_t mbedtls_mpi_uint; -#define MBEDTLS_MPI_UINT_MAX 18446744073709551615UL +#define MBEDTLS_MPI_UINT_MAX 18446744073709551615U #endif #endif /* !MBEDTLS_HAVE_INT32 */ @@ -179,7 +179,7 @@ typedef uint64_t mbedtls_mpi_uint; #endif /* !MBEDTLS_HAVE_INT32 */ typedef int32_t mbedtls_mpi_sint; typedef uint32_t mbedtls_mpi_uint; -#define MBEDTLS_MPI_UINT_MAX 4294967295UL +#define MBEDTLS_MPI_UINT_MAX 4294967295U #if !defined(MBEDTLS_NO_UDBL_DIVISION) typedef uint64_t mbedtls_t_udbl; #define MBEDTLS_HAVE_UDBL From eed55c6c94ba4fdc654bb6d305b5297e10c611b9 Mon Sep 17 00:00:00 2001 From: Agathiyan Bragadeesh Date: Wed, 19 Jul 2023 11:08:02 +0100 Subject: [PATCH 7/7] Use defined macros for MBEDTLS_MPI_UINT_MAX Signed-off-by: Agathiyan Bragadeesh --- include/mbedtls/bignum.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/include/mbedtls/bignum.h b/include/mbedtls/bignum.h index 6fb79cf2bf..71cf49a5d5 100644 --- a/include/mbedtls/bignum.h +++ b/include/mbedtls/bignum.h @@ -129,7 +129,7 @@ #endif /* !MBEDTLS_HAVE_INT64 */ typedef int64_t mbedtls_mpi_sint; typedef uint64_t mbedtls_mpi_uint; -#define MBEDTLS_MPI_UINT_MAX 18446744073709551615U +#define MBEDTLS_MPI_UINT_MAX UINT64_MAX #elif defined(__GNUC__) && ( \ defined(__amd64__) || defined(__x86_64__) || \ defined(__ppc64__) || defined(__powerpc64__) || \ @@ -142,7 +142,7 @@ typedef uint64_t mbedtls_mpi_uint; #endif /* MBEDTLS_HAVE_INT64 */ typedef int64_t mbedtls_mpi_sint; typedef uint64_t mbedtls_mpi_uint; -#define MBEDTLS_MPI_UINT_MAX 18446744073709551615U +#define MBEDTLS_MPI_UINT_MAX UINT64_MAX #if !defined(MBEDTLS_NO_UDBL_DIVISION) /* mbedtls_t_udbl defined as 128-bit unsigned int */ typedef unsigned int mbedtls_t_udbl __attribute__((mode(TI))); @@ -158,7 +158,7 @@ typedef unsigned int mbedtls_t_udbl __attribute__((mode(TI))); #endif /* !MBEDTLS_HAVE_INT64 */ typedef int64_t mbedtls_mpi_sint; typedef uint64_t mbedtls_mpi_uint; -#define MBEDTLS_MPI_UINT_MAX 18446744073709551615U +#define MBEDTLS_MPI_UINT_MAX UINT64_MAX #if !defined(MBEDTLS_NO_UDBL_DIVISION) /* mbedtls_t_udbl defined as 128-bit unsigned int */ typedef __uint128_t mbedtls_t_udbl; @@ -168,7 +168,7 @@ typedef __uint128_t mbedtls_t_udbl; /* Force 64-bit integers with unknown compiler */ typedef int64_t mbedtls_mpi_sint; typedef uint64_t mbedtls_mpi_uint; -#define MBEDTLS_MPI_UINT_MAX 18446744073709551615U +#define MBEDTLS_MPI_UINT_MAX UINT64_MAX #endif #endif /* !MBEDTLS_HAVE_INT32 */ @@ -179,7 +179,7 @@ typedef uint64_t mbedtls_mpi_uint; #endif /* !MBEDTLS_HAVE_INT32 */ typedef int32_t mbedtls_mpi_sint; typedef uint32_t mbedtls_mpi_uint; -#define MBEDTLS_MPI_UINT_MAX 4294967295U +#define MBEDTLS_MPI_UINT_MAX UINT32_MAX #if !defined(MBEDTLS_NO_UDBL_DIVISION) typedef uint64_t mbedtls_t_udbl; #define MBEDTLS_HAVE_UDBL