go.crypto/openpgp: don't corrupt encrypted private key when decryption fails.

Since the decryption was done in place, if an incorrect key was given,
the encrypted data would be corrupted.

Fixes golang/go#8439.

R=bradfitz
CC=golang-codereviews
https://golang.org/cl/115550043
This commit is contained in:
Adam Langley
2014-10-25 11:19:44 -07:00
parent 7780ec1765
commit 3e2271302c
2 changed files with 8 additions and 2 deletions
+2 -2
View File
@@ -196,8 +196,8 @@ func (pk *PrivateKey) Decrypt(passphrase []byte) error {
block := pk.cipher.new(key)
cfb := cipher.NewCFBDecrypter(block, pk.iv)
data := pk.encryptedData
cfb.XORKeyStream(data, data)
data := make([]byte, len(pk.encryptedData))
cfb.XORKeyStream(data, pk.encryptedData)
if pk.sha1Checksum {
if len(data) < sha1.Size {
+6
View File
@@ -38,6 +38,12 @@ func TestPrivateKeyRead(t *testing.T) {
continue
}
err = privKey.Decrypt([]byte("wrong password"))
if err == nil {
t.Errorf("#%d: decrypted with incorrect key", i)
continue
}
err = privKey.Decrypt([]byte("testing"))
if err != nil {
t.Errorf("#%d: failed to decrypt: %s", i, err)