[PATCH libgcrypt] ecc: Fix keygrip of native 25519 points starting with 0x02 or 0x03.

David Seifert soap at gentoo.org
Thu Nov 12 12:53:54 CET 2026


* cipher/ecc.c (compute_keygrip): Do not try to uncompress the point
of a 255 bit Edwards or Montgomery curve.
* tests/keygrip.c (key_grips): Add test vectors.
--

Without flags, the q of an Ed25519, Curve25519 or ietf25 key is the
native 32 byte encoding of the point. This is the form of keys from
X.509 certificates (RFC 8410) as returned by libksba and of RFC 9580
X25519 keys in GnuPG. If the first byte of such a point is 0x02 or
0x03, compute_keygrip takes it for a compressed Weierstrass point and
tries to uncompress it. This fails because p is not 3 mod 4 for these
curves, and thus gcry_pk_get_keygrip fails for about 2 in 256 keys.

Skip the uncompression for these curves and hash the point as it is.
This yields the same keygrip as the flagged form of the key. Only
keygrips which could not be computed before are affected. The 448 bit
curves are left alone: X448 and flagged Ed448 points starting with
0x02 or 0x03 do get uncompressed and changing that would change
existing keygrips.

Signed-off-by: David Seifert <soap at gentoo.org>
---
 cipher/ecc.c    |  8 ++++++
 tests/keygrip.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 78 insertions(+)

diff --git a/cipher/ecc.c b/cipher/ecc.c
index 8545145d..aa6320f2 100644
--- a/cipher/ecc.c
+++ b/cipher/ecc.c
@@ -1820,6 +1820,14 @@ compute_keygrip (gcry_md_hd_t md, gcry_sexp_t keyparms)
         }
       maybe_uncompress = 0;
     }
+  else if (model != MPI_EC_WEIERSTRASS && mpi_get_nbits (values[0]) == 255)
+    /* Without flags, an Ed25519, Curve25519 or ietf25 point is in
+       its native encoding (e.g. X.509 per RFC 8410 or RFC 9580
+       X25519) and a leading 0x02 or 0x03 is part of the point.
+       Trying to uncompress it would fail.  The 448 bit curves are
+       left alone because X448 and flagged Ed448 points do get
+       uncompressed and changing that would change their keygrips.  */
+    maybe_uncompress = 0;
   else
     maybe_uncompress = 1;
 
diff --git a/tests/keygrip.c b/tests/keygrip.c
index 503a2c05..6e67cf56 100644
--- a/tests/keygrip.c
+++ b/tests/keygrip.c
@@ -254,6 +254,76 @@ static struct
       "\x33\x23\x98\xF5\xD4\x80\x67\x7D\xF4\x9C",
       1
     },
+    /* Native Ed25519 and Curve25519 points without flags, as in X.509
+     * (RFC 8410) or RFC 9580 X25519, which start with 0x02 or 0x03.
+     * The keygrips must match those of the flagged forms.  */
+    { /* Ed25519 (X.509) */
+      GCRY_PK_ECC,
+      "(public-key"
+      " (ecc"
+      " (curve \"1.3.101.112\")"
+      " (q #02C94DD0731AC5162EF1FBFD812893E9AB92DFAC2BB0D2C39CA43BB6DD33BA6E#)"
+      " ))",
+      "\x00\xC7\x93\x46\x46\xE9\x00\xDA\x9F\x00"
+      "\x14\x81\x62\x7D\x1A\x76\xEB\x30\xB9\x89"
+    },
+    { /* Ed25519+EdDSA, same key */
+      GCRY_PK_ECC,
+      "(public-key"
+      " (ecc"
+      " (curve Ed25519)(flags eddsa)"
+      " (q #40"
+      "     02C94DD0731AC5162EF1FBFD812893E9AB92DFAC2BB0D2C39CA43BB6DD33BA6E#)"
+      " ))",
+      "\x00\xC7\x93\x46\x46\xE9\x00\xDA\x9F\x00"
+      "\x14\x81\x62\x7D\x1A\x76\xEB\x30\xB9\x89"
+    },
+    { /* Cv25519 (X.509) */
+      GCRY_PK_ECC,
+      "(public-key"
+      " (ecc"
+      " (curve \"1.3.101.110\")"
+      " (q #033D6693A55C7408AA43A4FF49F3CB82AD48AD4D70212BCA51B4ED0FA678B038#)"
+      " ))",
+      "\xCE\xF4\x74\x27\x54\xA4\xA0\x13\xE2\xD6"
+      "\xFB\x0D\xEE\xCE\x90\x5F\x56\x4B\xC9\x68",
+      1
+    },
+    { /* Cv25519, same key */
+      GCRY_PK_ECC,
+      "(public-key"
+      " (ecc"
+      " (curve Curve25519)(flags djb-tweak)"
+      " (q #40"
+      "     033D6693A55C7408AA43A4FF49F3CB82AD48AD4D70212BCA51B4ED0FA678B038#)"
+      " ))",
+      "\xCE\xF4\x74\x27\x54\xA4\xA0\x13\xE2\xD6"
+      "\xFB\x0D\xEE\xCE\x90\x5F\x56\x4B\xC9\x68",
+      1
+    },
+    { /* X25519 (RFC 9580) */
+      GCRY_PK_ECC,
+      "(public-key"
+      " (ecc"
+      " (curve ietf25)"
+      " (q #02F682704BF3DAEB87ECC31430DA17D783CCBD6A5B587A0EA1C6DEDBC020592F#)"
+      " ))",
+      "\x96\x8E\x51\xF9\xF0\x72\x6B\x29\xDD\x61"
+      "\xD6\xBA\x8D\xF1\x0A\x39\x58\x8C\x76\xAD",
+      1
+    },
+    { /* X25519 with flag, same key */
+      GCRY_PK_ECC,
+      "(public-key"
+      " (ecc"
+      " (curve ietf25)(flags djb-tweak)"
+      " (q #40"
+      "     02F682704BF3DAEB87ECC31430DA17D783CCBD6A5B587A0EA1C6DEDBC020592F#)"
+      " ))",
+      "\x96\x8E\x51\xF9\xF0\x72\x6B\x29\xDD\x61"
+      "\xD6\xBA\x8D\xF1\x0A\x39\x58\x8C\x76\xAD",
+      1
+    },
     { /* Random key  */
       GCRY_PK_RSA,
       "(shadowed-private-key"
-- 
2.56.0



More information about the Gcrypt-devel mailing list