[PATCH] Validate PolynomialMod2 coefficients Thanks to Bing Shi
authorJeffrey Walton <noloader@gmail.com>
Mon, 20 Nov 2023 06:36:15 +0000 (01:36 -0500)
committerLaszlo Boszormenyi (GCS) <gcs@debian.org>
Tue, 29 Apr 2025 19:12:05 +0000 (21:12 +0200)
Gbp-Pq: Name CVE-2023-50980.patch

gf2n.cpp

index 8993baaed96987f4cdca0d8683a5be5eeb211c2b..87d9961b241bf9de5bc348e8e93f088c4dcec39d 100644 (file)
--- a/gf2n.cpp
+++ b/gf2n.cpp
@@ -135,9 +135,14 @@ PolynomialMod2 PolynomialMod2::Monomial(size_t i)
 \r
 PolynomialMod2 PolynomialMod2::Trinomial(size_t t0, size_t t1, size_t t2)\r
 {\r
+       // Asserts and checks due to Bing Shi\r
        CRYPTOPP_ASSERT(t0 > t1);\r
        CRYPTOPP_ASSERT(t1 > t2);\r
 \r
+       // The test is odd because of ECIES<EC2N>. The basis is t0, but the other coefficients are not in descending order.\r
+       if (t1 > t0 || t2 > t0)\r
+               throw InvalidArgument("PolynomialMod2: coefficients must be in descending order");\r
+\r
        PolynomialMod2 r((word)0, t0+1);\r
        r.SetBit(t0);\r
        r.SetBit(t1);\r
@@ -147,11 +152,16 @@ PolynomialMod2 PolynomialMod2::Trinomial(size_t t0, size_t t1, size_t t2)
 \r
 PolynomialMod2 PolynomialMod2::Pentanomial(size_t t0, size_t t1, size_t t2, size_t t3, size_t t4)\r
 {\r
+       // Asserts and checks due to Bing Shi\r
        CRYPTOPP_ASSERT(t0 > t1);\r
        CRYPTOPP_ASSERT(t1 > t2);\r
        CRYPTOPP_ASSERT(t2 > t3);\r
        CRYPTOPP_ASSERT(t3 > t4);\r
 \r
+       // The test is odd because of ECIES<EC2N>. The basis is t0, but the other coefficients are not in descending order.\r
+       if (t1 > t0 || t2 > t0 || t3 > t0 || t4 > t0)\r
+               throw InvalidArgument("PolynomialMod2: coefficients must be in descending order");\r
+\r
        PolynomialMod2 r((word)0, t0+1);\r
        r.SetBit(t0);\r
        r.SetBit(t1);\r
@@ -663,7 +673,12 @@ GF2NT::GF2NT(unsigned int c0, unsigned int c1, unsigned int c2)
        , t0(c0), t1(c1)\r
        , result((word)0, m)\r
 {\r
+       // Asserts and checks due to Bing Shi\r
        CRYPTOPP_ASSERT(c0 > c1 && c1 > c2 && c2==0);\r
+\r
+       // The test is odd because of ECIES<EC2N>. The basis is c0, but the other coefficients are not in descending order.\r
+       if (c1 > c0 || c2 > c0)\r
+               throw InvalidArgument("GF2NT: coefficients must be in descending order");\r
 }\r
 \r
 const GF2NT::Element& GF2NT::MultiplicativeInverse(const Element &a) const\r
@@ -972,7 +987,12 @@ GF2NP * BERDecodeGF2NP(BufferedTransformation &bt)
 GF2NT233::GF2NT233(unsigned int c0, unsigned int c1, unsigned int c2)\r
        : GF2NT(c0, c1, c2)\r
 {\r
+       // Asserts and checks due to Bing Shi\r
        CRYPTOPP_ASSERT(c0 > c1 && c1 > c2 && c2==0);\r
+\r
+       // The test is odd because of ECIES<EC2N>. The basis is c0, but the other coefficients are not in descending order.\r
+       if (c1 > c0 || c2 > c0)\r
+               throw InvalidArgument("GF2NT: coefficients must be in descending order");\r
 }\r
 \r
 const GF2NT::Element& GF2NT233::Multiply(const Element &a, const Element &b) const\r