Public bug reported:
[ Impact ]
libgcrypt20 1.12.0-2ubuntu1.1 in resolute rejects valid Curve25519
secret keys in gcry_pk_testkey(), depending on the contents of the
heap. gpg-agent runs gcry_pk_testkey() when it first uses an OpenPGP
secret key it imported, and reports the failure as "Bad passphrase"
for a key protected by a passphrase and as "Bad secret key" otherwise.
Curve25519 (cv25519) is the encryption subkey of GnuPG's default key
type. A user who moves a key to a new machine, restores a backup, or
imports a key from another OpenPGP implementation can be refused even
though the passphrase is correct. Whether it happens depends on the
heap layout of gpg-agent, which changes with things like the length of
the GnuPG home path, so it looks random. In automated tests that create
temporary GnuPG homes, about one import in twenty failed, and for some
paths every import failed. Keys generated on the machine are not
affected, as gpg-agent does not test them. Ed25519 and RSA keys are not
affected.
Releases before 26.04 are not affected (noble has 1.10.3, questing has
1.11.0), and stonking and tenacious have 1.12.2, which contains the
fix. Upgrading from 24.04 LTS to 26.04 LTS therefore introduces it.
The cause: libgcrypt 1.12 added this to ec_mod() in mpi/ec.c,
if ((ec->flags & GCRYECC_FLAG_LEAST_LEAK))
w->nlimbs = ec->p->nlimbs;
which raises the limb count of W to that of P without making sure the
limbs are allocated. ecc_check_secret_key() sets that flag. In the
Montgomery branch of _gcry_mpi_ec_curve_point(), x is the x-coordinate
of G, which is 9 for Curve25519. ec_pow2() leaves x^2 in a two-limb
array, ec_mod() declares it four limbs long, and the following
ec_addm() reads limbs 2 and 3 past the allocation. check_secret_key()
then fails for about half of the values it finds there, with
"Bad check: Point 'G' does not belong to curve 'E'!".
Upstream tracked this as https://dev.gnupg.org/T8094 ("libgcrypt: EC
least leak failure") and released the fix in libgcrypt 1.12.1
(2026-02-20):
1468aaaa473d7f73623247580441bcc4aef07ee1
mpi:ec: Make sure to have MPI limbs in ECC.
740915ea5f20cfce21d182391389b26a462075c8
mpi:ec: Use mpi_new with NBITS, instead of mpi_alloc.
4aa07eeca100512e87117db8ef4b41cce12b10cb
cipher:ecc: Fix Weierstrass curve with PUBKEY_FLAG_PARAM.
The third commit is needed with the first: 1468aaaa47 changes
point_from_keyparam() to call mpi_point_new (ec->nbits), and ec is NULL
there for a Weierstrass curve given by its parameters. The package
already carries 949557945c (T8080, debian/patches/30_0002) from the
same release, but not these three. All three apply cleanly to the
1.12.0-2ubuntu1.1 source with its patches applied, and fix every
failure in the test plan below.
Please backport them to resolute.
[ Test Plan ]
The results marked "patched" below come from the 1.12.0-2ubuntu1.1
source with the three commits applied, rebuilt with autoreconf and the
package's configure flags (--enable-noexecstack
--enable-ld-version-script --enable-marvin-workaround) and loaded
through LD_LIBRARY_PATH. The same source rebuilt without the commits
fails exactly like the package.
1. Run the attached reproduce-gpg.sh. It generates one cv25519 key,
exports it, and imports it into 24 fresh GnuPG homes whose paths
differ only in length, decrypting the same message in each. With
1.12.0-2ubuntu1.1 on amd64:
oooooxxooooooooooooooooo
decrypted: 22, failed: 2 (gpg: decryption failed: Bad secret key)
Which homes fail depends on the machine, but stays the same on it.
Patched:
oooooooooooooooooooooooo
decrypted: 24, failed: 0
2. Build and run the attached testkey.c:
gcc -o testkey testkey.c -lgcrypt && ./testkey 400
It generates a Curve25519 and an Ed25519 key with gcry_pk_genkey()
and runs gcry_pk_testkey() on each 400 times, leaving the heap in a
different shape before every check. With 1.12.0-2ubuntu1.1:
libgcrypt 1.12.0
Curve25519 testkey refused 231 of 400 checks of one valid key
Ed25519 testkey refused 0 of 400 checks of one valid key
The Curve25519 count varies a little from run to run. Patched, both
refuse 0 of 400, in two runs.
3. "valgrind ./testkey 3" reports two "Invalid read of size 8", 0 and 8
bytes after a block of size 16, in _gcry_mpih_add_n() called from
_gcry_mpi_ec_curve_point() <- ecc_check_secret_key() <-
gcry_pk_testkey(), 6 errors in all. Patched: 0 errors.
4. libgcrypt's own test suite (make -C tests check) passes with and
without the commits: 39 passed, none failed, hashtest-6g and
hashtest-256g skipped, t-cv25519, t-x448, t-ed25519, t-ed448,
curves, keygen, pubkey and t-ecdsa among the passed. It does not
catch this bug, so items 1 to 3 are its regression test.
[ Where problems could occur ]
The commits change how the ECC code allocates MPIs and points:
mpi_new (0), mpi_alloc (0) and point_init() become allocations sized
for the curve, in ECDH, ECDSA, EdDSA, GOST, SM2, key generation and
point decoding. A mistake there would show as wrong results or crashes
in elliptic curve operations of any program using libgcrypt, gpg-agent
and gpgsm among them. The NULL dereference that 4aa07eeca1 fixes is a
mistake of that kind, which is why it belongs in the backport. The
changes have been in upstream releases since 1.12.1 (February 2026)
and are in the libgcrypt20 of stonking.
[ Other Info ]
gpg-agent maps a failure of gcry_pk_testkey() in do_unprotect()
(agent/cvt-openpgp.c, "The checksum may fail, thus we also check the
key itself") to GPG_ERR_BAD_PASSPHRASE, and convert_from_openpgp_main()
turns that into GPG_ERR_BAD_SECKEY for an unprotected key. That is why
users see "Bad passphrase" although the passphrase is correct.
Running gpg-agent under valgrind shows the same two invalid reads,
called from its key conversion. Configuring an agent log file or a
debug level changes the heap layout and often hides the failure.
A passphrase-protected cv25519 key, imported into GnuPG homes of 24
different path lengths, failed with "Bad passphrase" in 21 of them with
the package and in none patched. A second protected key failed in 1 of
24 with the package and in none patched.
Found with gnupg 2.4.8-4ubuntu3.1 and libgcrypt20 1.12.0-2ubuntu1.1 on
amd64.
** Affects: libgcrypt20 (Ubuntu)
Importance: Undecided
Status: New
** Tags: regression-release resolute
** Attachment added: "reproduce-gpg.sh"
https://bugs.launchpad.net/bugs/2174038/+attachment/6017191/+files/reproduce-gpg.sh
--
You received this bug notification because you are a member of Ubuntu
Bugs, which is subscribed to Ubuntu.
https://bugs.launchpad.net/bugs/2174038
Title:
gcry_pk_testkey randomly rejects valid Curve25519 keys, so gpg-agent
refuses imported cv25519 keys (T8094, fixed in 1.12.1)
To manage notifications about this bug go to:
https://bugs.launchpad.net/ubuntu/+source/libgcrypt20/+bug/2174038/+subscriptions
--
ubuntu-bugs mailing list
[email protected]
https://lists.ubuntu.com/mailman/listinfo/ubuntu-bugs