Skip to content

Commit

Permalink
ECDH with a v6 key must use the full fingerprint (#211)
Browse files Browse the repository at this point in the history
* Fix ECDH fingeprint size for v6 keys

Do not truncate the fingerprint to 20 bytes as for v5,
but rather use the whole fingerprint for v6

* Fix linter issues
  • Loading branch information
lubux authored Jul 2, 2024
1 parent 140b3d6 commit 3272cd7
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 12 deletions.
3 changes: 1 addition & 2 deletions openpgp/ecdh/ecdh.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,7 @@ func buildKey(pub *PublicKey, zb []byte, curveOID, fingerprint []byte, stripLead
if _, err := param.Write([]byte("Anonymous Sender ")); err != nil {
return nil, err
}
// For v5 keys, the 20 leftmost octets of the fingerprint are used.
if _, err := param.Write(fingerprint[:20]); err != nil {
if _, err := param.Write(fingerprint[:]); err != nil {
return nil, err
}
if param.Len()-len(curveOID) != 45 {
Expand Down
2 changes: 1 addition & 1 deletion openpgp/packet/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func (c *Config) S2K() *s2k.Config {
return nil
}
// for backwards compatibility
if c != nil && c.S2KCount > 0 && c.S2KConfig == nil {
if c.S2KCount > 0 && c.S2KConfig == nil {
return &s2k.Config{
S2KCount: c.S2KCount,
}
Expand Down
7 changes: 6 additions & 1 deletion openpgp/packet/encrypted_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,12 @@ func (e *EncryptedKey) Decrypt(priv *PrivateKey, config *Config) error {
vsG := e.encryptedMPI1.Bytes()
m := e.encryptedMPI2.Bytes()
oid := priv.PublicKey.oid.EncodedBytes()
b, err = ecdh.Decrypt(priv.PrivateKey.(*ecdh.PrivateKey), vsG, m, oid, priv.PublicKey.Fingerprint[:])
fp := priv.PublicKey.Fingerprint[:]
if priv.PublicKey.Version == 5 {
// For v5 the, the fingerprint must be restricted to 20 bytes
fp = fp[:20]
}
b, err = ecdh.Decrypt(priv.PrivateKey.(*ecdh.PrivateKey), vsG, m, oid, fp)
case PubKeyAlgoX25519:
b, err = x25519.Decrypt(priv.PrivateKey.(*x25519.PrivateKey), e.ephemeralPublicX25519, e.encryptedSession)
case PubKeyAlgoX448:
Expand Down
5 changes: 2 additions & 3 deletions openpgp/packet/public_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -910,8 +910,7 @@ func (pk *PublicKey) VerifyRevocationHashTag(sig *Signature) (err error) {
if err != nil {
return err
}
err = keyRevocationHash(pk, preparedHash)
if err != nil {
if err = keyRevocationHash(pk, preparedHash); err != nil {
return err
}
return VerifyHashTag(preparedHash, sig)
Expand All @@ -924,7 +923,7 @@ func (pk *PublicKey) VerifyRevocationSignature(sig *Signature) (err error) {
if err != nil {
return err
}
if keyRevocationHash(pk, preparedHash); err != nil {
if err = keyRevocationHash(pk, preparedHash); err != nil {
return err
}
return pk.VerifySignature(preparedHash, sig)
Expand Down
6 changes: 1 addition & 5 deletions openpgp/v2/read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -775,11 +775,7 @@ func TestSymmetricAeadEaxOpenPGPJsMessage(t *testing.T) {
}

// Decrypt with key
var edp packet.EncryptedDataPacket
if err != nil {
t.Fatal(err)
}
edp = p.(*packet.AEADEncrypted)
edp := p.(*packet.AEADEncrypted)
rc, err := edp.Decrypt(packet.CipherFunction(0), key)
if err != nil {
panic(err)
Expand Down

0 comments on commit 3272cd7

Please sign in to comment.