Add unit tests for decryption of aead packets
This commit is contained in:
parent
4cea9a0940
commit
373e1ad526
4 changed files with 96 additions and 0 deletions
38
crypto/keyring_message_test.go
Normal file
38
crypto/keyring_message_test.go
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
package crypto
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestAEADKeyRingDecryption(t *testing.T) {
|
||||
pgpMessageData, err := ioutil.ReadFile("testdata/gpg2.3-aead-pgp-message.pgp")
|
||||
if err != nil {
|
||||
t.Fatal("Expected no error when reading message data, got:", err)
|
||||
}
|
||||
pgpMessage := NewPGPMessage(pgpMessageData)
|
||||
|
||||
aeadKey, err := NewKeyFromArmored(readTestFile("gpg2.3-aead-test-key.asc", false))
|
||||
if err != nil {
|
||||
t.Fatal("Expected no error when unarmoring key, got:", err)
|
||||
}
|
||||
|
||||
aeadKeyUnlocked, err := aeadKey.Unlock([]byte("test"))
|
||||
if err != nil {
|
||||
t.Fatal("Expected no error when unlocking, got:", err)
|
||||
}
|
||||
kR, err := NewKeyRing(aeadKeyUnlocked)
|
||||
if err != nil {
|
||||
t.Fatal("Expected no error when creating the keyring, got:", err)
|
||||
}
|
||||
defer kR.ClearPrivateParams()
|
||||
|
||||
decrypted, err := kR.Decrypt(pgpMessage, nil, 0)
|
||||
if err != nil {
|
||||
t.Fatal("Expected no error when decrypting, got:", err)
|
||||
}
|
||||
|
||||
assert.Exactly(t, "hello world\n", decrypted.GetString())
|
||||
}
|
||||
|
|
@ -4,6 +4,7 @@ import (
|
|||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"testing"
|
||||
|
||||
"github.com/ProtonMail/gopenpgp/v2/constants"
|
||||
|
|
@ -344,3 +345,42 @@ func TestAsymmetricKeyPacketDecryptionFailure(t *testing.T) {
|
|||
_, err = ukr.DecryptSessionKey(keyPacket)
|
||||
assert.Error(t, err, "gopenpgp: unable to decrypt session key")
|
||||
}
|
||||
|
||||
func TestAEADDataPacketDecryption(t *testing.T) {
|
||||
pgpMessageData, err := ioutil.ReadFile("testdata/gpg2.3-aead-pgp-message.pgp")
|
||||
if err != nil {
|
||||
t.Fatal("Expected no error when reading message data, got:", err)
|
||||
}
|
||||
pgpMessage := NewPGPMessage(pgpMessageData)
|
||||
|
||||
split, err := pgpMessage.SplitMessage()
|
||||
if err != nil {
|
||||
t.Fatal("Expected no error when splitting, got:", err)
|
||||
}
|
||||
|
||||
aeadKey, err := NewKeyFromArmored(readTestFile("gpg2.3-aead-test-key.asc", false))
|
||||
if err != nil {
|
||||
t.Fatal("Expected no error when unarmoring key, got:", err)
|
||||
}
|
||||
|
||||
aeadKeyUnlocked, err := aeadKey.Unlock([]byte("test"))
|
||||
if err != nil {
|
||||
t.Fatal("Expected no error when unlocking, got:", err)
|
||||
}
|
||||
kR, err := NewKeyRing(aeadKeyUnlocked)
|
||||
if err != nil {
|
||||
t.Fatal("Expected no error when creating the keyring, got:", err)
|
||||
}
|
||||
defer kR.ClearPrivateParams()
|
||||
sessionKey, err := kR.DecryptSessionKey(split.GetBinaryKeyPacket())
|
||||
if err != nil {
|
||||
t.Fatal("Expected no error when decrypting session key, got:", err)
|
||||
}
|
||||
|
||||
decrypted, err := sessionKey.Decrypt(split.GetBinaryDataPacket())
|
||||
if err != nil {
|
||||
t.Fatal("Expected no error when decrypting, got:", err)
|
||||
}
|
||||
|
||||
assert.Exactly(t, "hello world\n", decrypted.GetString())
|
||||
}
|
||||
|
|
|
|||
1
crypto/testdata/gpg2.3-aead-pgp-message.pgp
vendored
Normal file
1
crypto/testdata/gpg2.3-aead-pgp-message.pgp
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
„^Y7p²}‹•@M¶MŸRŠtÀ·ûüÉ‘öÝ–“ˆ9” ™Š
âäÓ#À0%;׬ÛÐj4# R<>¡Îs·Rü“æ¬AZA"Ĩ?f/½L]³…‡eÑ£›Ô^ ‰€k³½ŸE8¹^ýú¶–½†“Ï‚õ8›&x<>#ã2n‹QÝ
±×s¬¶½¾AСð¾Ô£éëB6‡Ù<>±Òj~j¢1%Ïi´À6sæ+-˜I`ùp÷€®
|
||||
17
crypto/testdata/gpg2.3-aead-test-key.asc
vendored
Normal file
17
crypto/testdata/gpg2.3-aead-test-key.asc
vendored
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
-----BEGIN PGP PRIVATE KEY BLOCK-----
|
||||
|
||||
lIYEYqiY8xYJKwYBBAHaRw8BAQdAlnAqgX8/hgDonAw/IpekRQwfVJmRWZUbVaK9
|
||||
SpL7qbr+BwMCweVnv2EC0AD/0ZzV/9F9qVc8P9kwBXsL2PZTSMLTHPlv9Wp4ny9X
|
||||
LH13AjR8+SbiX+FZxjBqYMfzQE/vMoSDgrOqEwToZM+K8Xczitox+LQ1YWVhZC1r
|
||||
ZXkgKHRoaXMgaXMgYSB0ZXN0IGtleSkgPGFlYWQta2V5QHByb3Rvbi5ibGFjaz6I
|
||||
kgQTFgoAOhYhBLTRCn0YEvaHOC8wd5z6DYBV4wK2BQJiqJjzAhsDBQsJCAcCAiIC
|
||||
BhUKCQgLAgMWAgECHgcCF4AACgkQnPoNgFXjArYTOwEApydkVgypRciM+4hOw4e4
|
||||
JrPulK90nKTt/ETH1idPicIBANke01I2pKoTj8dmzq5imE4+sDsKGNEdiksEYMqd
|
||||
Da8BnIsEYqiY8xIKKwYBBAGXVQEFAQEHQJBylc4dtR6ea1yu63TR8+tRpfGxIBR+
|
||||
58mXXJ+yfX9dAwEIB/4HAwJtMIaMnD5Nbf9BohXuc0kph8vqzqhMhKahUeBD4B+P
|
||||
1Gl4nH3BqyMCBS8bWhuYS5SmFdree1nny4aRI+veEB8aCLKDwqvlinh2N/z5Kh2v
|
||||
iHgEGBYKACAWIQS00Qp9GBL2hzgvMHec+g2AVeMCtgUCYqiY8wIbDAAKCRCc+g2A
|
||||
VeMCti86AQDjvVtf8P/pPSpC3KrrKTEZOW+x3mCZJhhUcQkTXlxRTgEArLOPU6kj
|
||||
vWc1uLyrizai9gxpkMZY9oT1BOMkp0wDdgI=
|
||||
=pBZD
|
||||
-----END PGP PRIVATE KEY BLOCK-----
|
||||
Loading…
Add table
Add a link
Reference in a new issue