[crypto] openpgp/elgamal: fix index out of range panic in Decrypt when ciphertext is zero

5 views
Skip to first unread message

Gerrit Bot (Gerrit)

unread,
Jun 3, 2026, 12:45:08 AM (4 days ago) Jun 3
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Gerrit Bot has uploaded the change for review

Commit message

openpgp/elgamal: fix index out of range panic in Decrypt when ciphertext is zero

Decrypt panics when the recovered value s equals zero because
s.Bytes() returns an empty slice and em[0] is indexed without
a bounds check.

An attacker can force s=0 by supplying c2=0 in a crafted
OpenPGP PKESK packet, causing a denial of service.

Fix: check len(em) == 0 before indexing and return an error.

Fixes: https://issuetracker.google.com/issues/519383708
Change-Id: Ie77e83fa62a655d32369cf54e42e626b6e2a0ce8
GitHub-Last-Rev: dcecbae40525341d0043cdd5d23cd08ca3768b4c
GitHub-Pull-Request: golang/crypto#348

Change diff

diff --git a/openpgp/elgamal/elgamal.go b/openpgp/elgamal/elgamal.go
index f922bdb..17588ed 100644
--- a/openpgp/elgamal/elgamal.go
+++ b/openpgp/elgamal/elgamal.go
@@ -89,6 +89,9 @@
s.Mod(s, priv.P)
em := s.Bytes()

+ if len(em) == 0 {
+ return nil, errors.New("elgamal: decryption error")
+ }
firstByteIsTwo := subtle.ConstantTimeByteEq(em[0], 2)

// The remainder of the plaintext must be a string of non-zero random
diff --git a/openpgp/elgamal/elgamal_test.go b/openpgp/elgamal/elgamal_test.go
index 9f0a854..fd03ef9 100644
--- a/openpgp/elgamal/elgamal_test.go
+++ b/openpgp/elgamal/elgamal_test.go
@@ -62,3 +62,14 @@
t.Errorf("unexpected success decrypting")
}
}
+
+func TestDecryptZeroCiphertext(t *testing.T) {
+ priv, err := GenerateKey(rand.Reader, 1024)
+ if err != nil {
+ t.Fatal(err)
+ }
+ _, err = Decrypt(priv, big.NewInt(2), big.NewInt(0))
+ if err == nil {
+ t.Fatal("expected error, got nil")
+ }
+}

Change information

Files:
  • M openpgp/elgamal/elgamal.go
  • M openpgp/elgamal/elgamal_test.go
Change size: S
Delta: 2 files changed, 14 insertions(+), 0 deletions(-)
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: crypto
Gerrit-Branch: master
Gerrit-Change-Id: Ie77e83fa62a655d32369cf54e42e626b6e2a0ce8
Gerrit-Change-Number: 786500
Gerrit-PatchSet: 1
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

Gopher Robot (Gerrit)

unread,
Jun 3, 2026, 12:45:10 AM (4 days ago) Jun 3
to Gerrit Bot, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Gopher Robot added 1 comment

Patchset-level comments
File-level comment, Patchset 1 (Latest):
Gopher Robot . unresolved

I spotted some possible problems with your PR:

  1. You usually need to reference a bug number for all but trivial or cosmetic fixes. For the crypto repo, the format is usually 'Fixes golang/go#12345' or 'Updates golang/go#12345' at the end of the commit message. Should you have a bug reference?

Please address any problems by updating the GitHub PR.

When complete, mark this comment as 'Done' and click the [blue 'Reply' button](https://go.dev/wiki/GerritBot#i-left-a-reply-to-a-comment-in-gerrit-but-no-one-but-me-can-see-it) above. These findings are based on heuristics; if a finding does not apply, briefly reply here saying so.

To update the commit title or commit message body shown here in Gerrit, you must edit the GitHub PR title and PR description (the first comment) in the GitHub web interface using the 'Edit' button or 'Edit' menu entry there. Note: pushing a new commit to the PR will not automatically update the commit message used by Gerrit.

For more details, see:

(In general for Gerrit code reviews, the change author is expected to [log in to Gerrit](https://go-review.googlesource.com/login/) with a Gmail or other Google account and then close out each piece of feedback by marking it as 'Done' if implemented as suggested or otherwise reply to each review comment. See the [Review](https://go.dev/doc/contribute#review) section of the Contributing Guide for details.)

Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: crypto
    Gerrit-Branch: master
    Gerrit-Change-Id: Ie77e83fa62a655d32369cf54e42e626b6e2a0ce8
    Gerrit-Change-Number: 786500
    Gerrit-PatchSet: 1
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-Comment-Date: Wed, 03 Jun 2026 04:45:05 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    unsatisfied_requirement
    open
    diffy

    Gopher Robot (Gerrit)

    unread,
    Jun 3, 2026, 12:49:21 AM (4 days ago) Jun 3
    to Gerrit Bot, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

    Message from Gopher Robot

    Congratulations on opening your first change. Thank you for your contribution!

    Next steps:
    A maintainer will review your change and provide feedback. See
    https://go.dev/doc/contribute#review for more info and tips to get your
    patch through code review.

    Most changes in the Go project go through a few rounds of revision. This can be
    surprising to people new to the project. The careful, iterative review process
    is our way of helping mentor contributors and ensuring that their contributions
    have a lasting impact.

    During May-July and Nov-Jan the Go project is in a code freeze, during which
    little code gets reviewed or merged. If a reviewer responds with a comment like
    R=go1.11 or adds a tag like "wait-release", it means that this CL will be
    reviewed as part of the next development cycle. See https://go.dev/s/release
    for more details.

    Open in Gerrit

    Related details

    Attention set is empty
    Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: crypto
    Gerrit-Branch: master
    Gerrit-Change-Id: Ie77e83fa62a655d32369cf54e42e626b6e2a0ce8
    Gerrit-Change-Number: 786500
    Gerrit-PatchSet: 1
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-Comment-Date: Wed, 03 Jun 2026 04:49:18 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: No
    unsatisfied_requirement
    open
    diffy

    Nasanbat (Gerrit)

    unread,
    Jun 3, 2026, 11:21:33 PM (3 days ago) Jun 3
    to Gerrit Bot, goph...@pubsubhelper.golang.org, Roland Shoemaker, Daniel McCarney, Filippo Valsorda, Gopher Robot, golang-co...@googlegroups.com
    Attention needed from Daniel McCarney, Filippo Valsorda and Roland Shoemaker

    Nasanbat added 1 comment

    Patchset-level comments
    Nasanbat . resolved

    Done

    Open in Gerrit

    Related details

    Attention is currently required from:
    • Daniel McCarney
    • Filippo Valsorda
    • Roland Shoemaker
    Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: crypto
    Gerrit-Branch: master
    Gerrit-Change-Id: Ie77e83fa62a655d32369cf54e42e626b6e2a0ce8
    Gerrit-Change-Number: 786500
    Gerrit-PatchSet: 1
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-Reviewer: Daniel McCarney <dan...@binaryparadox.net>
    Gerrit-Reviewer: Filippo Valsorda <fil...@golang.org>
    Gerrit-Reviewer: Roland Shoemaker <rol...@golang.org>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-CC: Nasanbat <nasaanas...@gmail.com>
    Gerrit-Attention: Roland Shoemaker <rol...@golang.org>
    Gerrit-Attention: Filippo Valsorda <fil...@golang.org>
    Gerrit-Attention: Daniel McCarney <dan...@binaryparadox.net>
    Gerrit-Comment-Date: Thu, 04 Jun 2026 03:21:24 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    unsatisfied_requirement
    open
    diffy

    Gerrit Bot (Gerrit)

    unread,
    Jun 3, 2026, 11:23:48 PM (3 days ago) Jun 3
    to Nasanbat, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
    Attention needed from Daniel McCarney, Filippo Valsorda and Roland Shoemaker

    Gerrit Bot uploaded new patchset

    Gerrit Bot uploaded patch set #2 to this change.
    Open in Gerrit

    Related details

    Attention is currently required from:
    • Daniel McCarney
    • Filippo Valsorda
    • Roland Shoemaker
    Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: newpatchset
    Gerrit-Project: crypto
    Gerrit-Branch: master
    Gerrit-Change-Id: Ie77e83fa62a655d32369cf54e42e626b6e2a0ce8
    Gerrit-Change-Number: 786500
    Gerrit-PatchSet: 2
    unsatisfied_requirement
    open
    diffy

    Alan Donovan (Gerrit)

    unread,
    Jun 4, 2026, 1:58:57 PM (2 days ago) Jun 4
    to Nasanbat, Gerrit Bot, goph...@pubsubhelper.golang.org, Roland Shoemaker, Daniel McCarney, Filippo Valsorda, Gopher Robot, golang-co...@googlegroups.com
    Attention needed from Daniel McCarney, Filippo Valsorda and Roland Shoemaker

    Alan Donovan added 1 comment

    Commit Message
    Line 18, Patchset 2 (Latest):Fixes golang/go#79799
    Alan Donovan . unresolved

    Wrong issue?

    Open in Gerrit

    Related details

    Attention is currently required from:
    • Daniel McCarney
    • Filippo Valsorda
    • Roland Shoemaker
    Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: crypto
    Gerrit-Branch: master
    Gerrit-Change-Id: Ie77e83fa62a655d32369cf54e42e626b6e2a0ce8
    Gerrit-Change-Number: 786500
    Gerrit-PatchSet: 2
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-Reviewer: Daniel McCarney <dan...@binaryparadox.net>
    Gerrit-Reviewer: Filippo Valsorda <fil...@golang.org>
    Gerrit-Reviewer: Roland Shoemaker <rol...@golang.org>
    Gerrit-CC: Alan Donovan <adon...@google.com>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-CC: Nasanbat <nasaanas...@gmail.com>
    Gerrit-Attention: Roland Shoemaker <rol...@golang.org>
    Gerrit-Attention: Filippo Valsorda <fil...@golang.org>
    Gerrit-Attention: Daniel McCarney <dan...@binaryparadox.net>
    Gerrit-Comment-Date: Thu, 04 Jun 2026 17:58:53 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    unsatisfied_requirement
    open
    diffy

    Gerrit Bot (Gerrit)

    unread,
    Jun 4, 2026, 10:27:24 PM (2 days ago) Jun 4
    to Nasanbat, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
    Attention needed from Daniel McCarney, Filippo Valsorda and Roland Shoemaker

    Gerrit Bot uploaded new patchset

    Gerrit Bot uploaded patch set #3 to this change.
    Open in Gerrit

    Related details

    Attention is currently required from:
    • Daniel McCarney
    • Filippo Valsorda
    • Roland Shoemaker
    Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: newpatchset
    Gerrit-Project: crypto
    Gerrit-Branch: master
    Gerrit-Change-Id: Ie77e83fa62a655d32369cf54e42e626b6e2a0ce8
    Gerrit-Change-Number: 786500
    Gerrit-PatchSet: 3
    unsatisfied_requirement
    open
    diffy

    Nasanbat (Gerrit)

    unread,
    Jun 6, 2026, 8:25:34 PM (5 hours ago) Jun 6
    to Gerrit Bot, goph...@pubsubhelper.golang.org, Alan Donovan, Roland Shoemaker, Daniel McCarney, Filippo Valsorda, Gopher Robot, golang-co...@googlegroups.com
    Attention needed from Alan Donovan, Daniel McCarney, Filippo Valsorda and Roland Shoemaker

    Nasanbat added 2 comments

    Patchset-level comments
    File-level comment, Patchset 1:
    Gopher Robot . resolved

    I spotted some possible problems with your PR:

      1. You usually need to reference a bug number for all but trivial or cosmetic fixes. For the crypto repo, the format is usually 'Fixes golang/go#12345' or 'Updates golang/go#12345' at the end of the commit message. Should you have a bug reference?

    Please address any problems by updating the GitHub PR.

    When complete, mark this comment as 'Done' and click the [blue 'Reply' button](https://go.dev/wiki/GerritBot#i-left-a-reply-to-a-comment-in-gerrit-but-no-one-but-me-can-see-it) above. These findings are based on heuristics; if a finding does not apply, briefly reply here saying so.

    To update the commit title or commit message body shown here in Gerrit, you must edit the GitHub PR title and PR description (the first comment) in the GitHub web interface using the 'Edit' button or 'Edit' menu entry there. Note: pushing a new commit to the PR will not automatically update the commit message used by Gerrit.

    For more details, see:

    (In general for Gerrit code reviews, the change author is expected to [log in to Gerrit](https://go-review.googlesource.com/login/) with a Gmail or other Google account and then close out each piece of feedback by marking it as 'Done' if implemented as suggested or otherwise reply to each review comment. See the [Review](https://go.dev/doc/contribute#review) section of the Contributing Guide for details.)

    Nasanbat

    Done

    Commit Message
    Line 18, Patchset 2:Fixes golang/go#79799
    Alan Donovan . resolved

    Wrong issue?

    Nasanbat

    You're right. golang/go#79799 was incorrect — that issue does not
    exist. I have created the correct issue: golang/go#79841

    Updated the commit message accordingly.

    Open in Gerrit

    Related details

    Attention is currently required from:
    • Alan Donovan
    • Daniel McCarney
    • Filippo Valsorda
    • Roland Shoemaker
      Submit Requirements:
        • requirement is not satisfiedCode-Review
        • requirement satisfiedNo-Unresolved-Comments
        • requirement is not satisfiedReview-Enforcement
        • requirement is not satisfiedTryBots-Pass
        Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
        Gerrit-MessageType: comment
        Gerrit-Project: crypto
        Gerrit-Branch: master
        Gerrit-Change-Id: Ie77e83fa62a655d32369cf54e42e626b6e2a0ce8
        Gerrit-Change-Number: 786500
        Gerrit-PatchSet: 3
        Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
        Gerrit-Reviewer: Daniel McCarney <dan...@binaryparadox.net>
        Gerrit-Reviewer: Filippo Valsorda <fil...@golang.org>
        Gerrit-Reviewer: Roland Shoemaker <rol...@golang.org>
        Gerrit-CC: Alan Donovan <adon...@google.com>
        Gerrit-CC: Gopher Robot <go...@golang.org>
        Gerrit-CC: Nasanbat <nasaanas...@gmail.com>
        Gerrit-Attention: Roland Shoemaker <rol...@golang.org>
        Gerrit-Attention: Filippo Valsorda <fil...@golang.org>
        Gerrit-Attention: Alan Donovan <adon...@google.com>
        Gerrit-Attention: Daniel McCarney <dan...@binaryparadox.net>
        Gerrit-Comment-Date: Sun, 07 Jun 2026 00:25:25 +0000
        Gerrit-HasComments: Yes
        Gerrit-Has-Labels: No
        Comment-In-Reply-To: Gopher Robot <go...@golang.org>
        Comment-In-Reply-To: Alan Donovan <adon...@google.com>
        unsatisfied_requirement
        satisfied_requirement
        open
        diffy

        Nasanbat (Gerrit)

        unread,
        Jun 6, 2026, 8:26:28 PM (5 hours ago) Jun 6
        to Gerrit Bot, goph...@pubsubhelper.golang.org, Alan Donovan, Roland Shoemaker, Daniel McCarney, Filippo Valsorda, Gopher Robot, golang-co...@googlegroups.com
        Attention needed from Alan Donovan, Daniel McCarney, Filippo Valsorda and Roland Shoemaker

        Nasanbat added 1 comment

        Patchset-level comments
        File-level comment, Patchset 3 (Latest):
        Nasanbat . resolved

        Acknowledged that x/crypto/openpgp is deprecated, but existing
        users remain vulnerable to this DoS. The fix is minimal and
        non-regressive.

        Gerrit-Comment-Date: Sun, 07 Jun 2026 00:26:22 +0000
        Gerrit-HasComments: Yes
        Gerrit-Has-Labels: No
        unsatisfied_requirement
        satisfied_requirement
        open
        diffy
        Reply all
        Reply to author
        Forward
        0 new messages