Skip to content

Commit

Permalink
fix: Address warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
lubux committed Oct 1, 2024
1 parent da0a0cf commit 3f6d02a
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 43 deletions.
4 changes: 2 additions & 2 deletions openpgp/forwarding.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,10 @@ func (e *Entity) NewForwardingEntity(
instance.ForwardeeFingerprint = forwardeeSubKey.PublicKey.Fingerprint

// 0x04 - This key may be used to encrypt communications.
forwardeeSubKey.Sig.FlagEncryptCommunications = false
forwardeeSubKey.Sig.FlagEncryptCommunications = true

// 0x08 - This key may be used to encrypt storage.
forwardeeSubKey.Sig.FlagEncryptStorage = false
forwardeeSubKey.Sig.FlagEncryptStorage = true

// 0x10 - The private component of this key may have been split by a secret-sharing mechanism.
forwardeeSubKey.Sig.FlagSplitKey = true
Expand Down
17 changes: 9 additions & 8 deletions openpgp/forwarding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"bytes"
"crypto/rand"
goerrors "errors"
"github.com/ProtonMail/go-crypto/openpgp/packet"
"golang.org/x/crypto/openpgp/armor"
"io"
"io/ioutil"
"strings"
"testing"

"github.com/ProtonMail/go-crypto/openpgp/packet"
"golang.org/x/crypto/openpgp/armor"
)

const forwardeeKey = `-----BEGIN PGP PRIVATE KEY BLOCK-----
Expand Down Expand Up @@ -59,7 +60,7 @@ func TestForwardingStatic(t *testing.T) {

dec, err := ioutil.ReadAll(m.decrypted)

if bytes.Compare(dec, []byte(forwardedPlaintext)) != 0 {
if !bytes.Equal(dec, []byte(forwardedPlaintext)) {
t.Fatal("forwarded decrypted does not match original")
}
}
Expand Down Expand Up @@ -89,11 +90,11 @@ func TestForwardingFull(t *testing.T) {
t.Fatalf("invalid number of instances, expected 1 got %d", len(instances))
}

if bytes.Compare(instances[0].ForwarderFingerprint, bobEntity.Subkeys[0].PublicKey.Fingerprint) != 0 {
if !bytes.Equal(instances[0].ForwarderFingerprint, bobEntity.Subkeys[0].PublicKey.Fingerprint) {
t.Fatalf("invalid forwarder key ID, expected: %x, got: %x", bobEntity.Subkeys[0].PublicKey.Fingerprint, instances[0].ForwarderFingerprint)
}

if bytes.Compare(instances[0].ForwardeeFingerprint, charlesEntity.Subkeys[0].PublicKey.Fingerprint) != 0 {
if !bytes.Equal(instances[0].ForwardeeFingerprint, charlesEntity.Subkeys[0].PublicKey.Fingerprint) {
t.Fatalf("invalid forwardee key ID, expected: %x, got: %x", charlesEntity.Subkeys[0].PublicKey.Fingerprint, instances[0].ForwardeeFingerprint)
}

Expand Down Expand Up @@ -123,7 +124,7 @@ func TestForwardingFull(t *testing.T) {
}
dec, err := ioutil.ReadAll(m.decrypted)

if bytes.Compare(dec, plaintext) != 0 {
if !bytes.Equal(dec, plaintext) {
t.Fatal("decrypted does not match original")
}

Expand All @@ -139,7 +140,7 @@ func TestForwardingFull(t *testing.T) {

dec, err = ioutil.ReadAll(m.decrypted)

if bytes.Compare(dec, plaintext) != 0 {
if !bytes.Equal(dec, plaintext) {
t.Fatal("forwarded decrypted does not match original")
}

Expand All @@ -161,7 +162,7 @@ func TestForwardingFull(t *testing.T) {

dec, err = ioutil.ReadAll(m.decrypted)

if bytes.Compare(dec, plaintext) != 0 {
if !bytes.Equal(dec, plaintext) {
t.Fatal("forwarded decrypted does not match original")
}
}
Expand Down
47 changes: 23 additions & 24 deletions openpgp/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import (
"github.com/ProtonMail/go-crypto/openpgp/eddsa"
"github.com/ProtonMail/go-crypto/openpgp/elgamal"
"github.com/ProtonMail/go-crypto/openpgp/errors"
"github.com/ProtonMail/go-crypto/openpgp/symmetric"
"github.com/ProtonMail/go-crypto/openpgp/internal/algorithm"
"github.com/ProtonMail/go-crypto/openpgp/packet"
"github.com/ProtonMail/go-crypto/openpgp/s2k"
"github.com/ProtonMail/go-crypto/openpgp/symmetric"
)

var hashes = []crypto.Hash{
Expand Down Expand Up @@ -1172,7 +1172,7 @@ func TestAddSubkeySerialized(t *testing.T) {

func TestAddHMACSubkey(t *testing.T) {
c := &packet.Config{
RSABits: 512,
RSABits: 512,
Algorithm: packet.ExperimentalPubKeyAlgoHMAC,
}

Expand All @@ -1187,7 +1187,7 @@ func TestAddHMACSubkey(t *testing.T) {
}

buf := bytes.NewBuffer(nil)
w, _ := armor.Encode(buf , "PGP PRIVATE KEY BLOCK", nil)
w, _ := armor.Encode(buf, "PGP PRIVATE KEY BLOCK", nil)
if err := entity.SerializePrivate(w, nil); err != nil {
t.Errorf("failed to serialize entity: %s", err)
}
Expand All @@ -1204,37 +1204,36 @@ func TestAddHMACSubkey(t *testing.T) {
generatedPublicKey := entity.Subkeys[1].PublicKey.PublicKey.(*symmetric.HMACPublicKey)
parsedPublicKey := key[0].Subkeys[1].PublicKey.PublicKey.(*symmetric.HMACPublicKey)

if bytes.Compare(parsedPrivateKey.Key, generatedPrivateKey.Key) != 0 {
if !bytes.Equal(parsedPrivateKey.Key, generatedPrivateKey.Key) {
t.Error("parsed wrong key")
}
if bytes.Compare(parsedPublicKey.Key, generatedPrivateKey.Key) != 0 {
if !bytes.Equal(parsedPublicKey.Key, generatedPrivateKey.Key) {
t.Error("parsed wrong key in public part")
}
if bytes.Compare(generatedPublicKey.Key, generatedPrivateKey.Key) != 0 {
if !bytes.Equal(generatedPublicKey.Key, generatedPrivateKey.Key) {
t.Error("generated Public and Private Key differ")
}

if bytes.Compare(parsedPrivateKey.HashSeed[:], generatedPrivateKey.HashSeed[:]) != 0 {
if !bytes.Equal(parsedPrivateKey.HashSeed[:], generatedPrivateKey.HashSeed[:]) {
t.Error("parsed wrong hash seed")
}

if parsedPrivateKey.PublicKey.Hash != generatedPrivateKey.PublicKey.Hash {
t.Error("parsed wrong cipher id")
}
if bytes.Compare(parsedPrivateKey.PublicKey.BindingHash[:], generatedPrivateKey.PublicKey.BindingHash[:]) != 0 {
if !bytes.Equal(parsedPrivateKey.PublicKey.BindingHash[:], generatedPrivateKey.PublicKey.BindingHash[:]) {
t.Error("parsed wrong binding hash")
}
}

func TestSerializeSymmetricSubkeyError(t *testing.T) {
entity, err := NewEntity("Golang Gopher", "Test Key", "[email protected]", &packet.Config{ RSABits: 1024})
entity, err := NewEntity("Golang Gopher", "Test Key", "[email protected]", &packet.Config{RSABits: 1024})
if err != nil {
t.Fatal(err)
}


buf := bytes.NewBuffer(nil)
w, _ := armor.Encode(buf , "PGP PRIVATE KEY BLOCK", nil)
w, _ := armor.Encode(buf, "PGP PRIVATE KEY BLOCK", nil)

entity.PrimaryKey.PubKeyAlgo = 100
err = entity.Serialize(w)
Expand All @@ -1251,7 +1250,7 @@ func TestSerializeSymmetricSubkeyError(t *testing.T) {

func TestAddAEADSubkey(t *testing.T) {
c := &packet.Config{
RSABits: 512,
RSABits: 512,
Algorithm: packet.ExperimentalPubKeyAlgoAEAD,
}
entity, err := NewEntity("Golang Gopher", "Test Key", "[email protected]", &packet.Config{RSABits: 1024})
Expand All @@ -1267,7 +1266,7 @@ func TestAddAEADSubkey(t *testing.T) {
generatedPrivateKey := entity.Subkeys[1].PrivateKey.PrivateKey.(*symmetric.AEADPrivateKey)

buf := bytes.NewBuffer(nil)
w, _ := armor.Encode(buf , "PGP PRIVATE KEY BLOCK", nil)
w, _ := armor.Encode(buf, "PGP PRIVATE KEY BLOCK", nil)
if err := entity.SerializePrivate(w, nil); err != nil {
t.Errorf("failed to serialize entity: %s", err)
}
Expand All @@ -1283,39 +1282,39 @@ func TestAddAEADSubkey(t *testing.T) {
generatedPublicKey := entity.Subkeys[1].PublicKey.PublicKey.(*symmetric.AEADPublicKey)
parsedPublicKey := key[0].Subkeys[1].PublicKey.PublicKey.(*symmetric.AEADPublicKey)

if bytes.Compare(parsedPrivateKey.Key, generatedPrivateKey.Key) != 0 {
if !bytes.Equal(parsedPrivateKey.Key, generatedPrivateKey.Key) {
t.Error("parsed wrong key")
}
if bytes.Compare(parsedPublicKey.Key, generatedPrivateKey.Key) != 0 {
if !bytes.Equal(parsedPublicKey.Key, generatedPrivateKey.Key) {
t.Error("parsed wrong key in public part")
}
if bytes.Compare(generatedPublicKey.Key, generatedPrivateKey.Key) != 0 {
if !bytes.Equal(generatedPublicKey.Key, generatedPrivateKey.Key) {
t.Error("generated Public and Private Key differ")
}

if bytes.Compare(parsedPrivateKey.HashSeed[:], generatedPrivateKey.HashSeed[:]) != 0 {
if !bytes.Equal(parsedPrivateKey.HashSeed[:], generatedPrivateKey.HashSeed[:]) {
t.Error("parsed wrong hash seed")
}

if parsedPrivateKey.PublicKey.Cipher.Id() != generatedPrivateKey.PublicKey.Cipher.Id() {
t.Error("parsed wrong cipher id")
}
if bytes.Compare(parsedPrivateKey.PublicKey.BindingHash[:], generatedPrivateKey.PublicKey.BindingHash[:]) != 0 {
if !bytes.Equal(parsedPrivateKey.PublicKey.BindingHash[:], generatedPrivateKey.PublicKey.BindingHash[:]) {
t.Error("parsed wrong binding hash")
}
}

func TestNoSymmetricKeySerialized(t *testing.T) {
aeadConfig := &packet.Config{
RSABits: 512,
DefaultHash: crypto.SHA512,
Algorithm: packet.ExperimentalPubKeyAlgoAEAD,
RSABits: 512,
DefaultHash: crypto.SHA512,
Algorithm: packet.ExperimentalPubKeyAlgoAEAD,
DefaultCipher: packet.CipherAES256,
}
hmacConfig := &packet.Config{
RSABits: 512,
DefaultHash: crypto.SHA512,
Algorithm: packet.ExperimentalPubKeyAlgoHMAC,
RSABits: 512,
DefaultHash: crypto.SHA512,
Algorithm: packet.ExperimentalPubKeyAlgoHMAC,
DefaultCipher: packet.CipherAES256,
}
entity, err := NewEntity("Golang Gopher", "Test Key", "[email protected]", &packet.Config{RSABits: 1024})
Expand Down
2 changes: 1 addition & 1 deletion openpgp/read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ func TestSymmetricAeadEaxOpenPGPJsMessage(t *testing.T) {
}

// Decrypt with key
var edp = p.(*packet.AEADEncrypted)
edp := p.(*packet.AEADEncrypted)
rc, err := edp.Decrypt(packet.CipherFunction(0), key)
if err != nil {
panic(err)
Expand Down
14 changes: 6 additions & 8 deletions openpgp/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,13 +266,13 @@ func TestNewEntity(t *testing.T) {

func TestEncryptWithAEAD(t *testing.T) {
c := &packet.Config{
Algorithm: packet.ExperimentalPubKeyAlgoAEAD,
Algorithm: packet.ExperimentalPubKeyAlgoAEAD,
DefaultCipher: packet.CipherAES256,
AEADConfig: &packet.AEADConfig{
DefaultMode: packet.AEADMode(1),
},
}
entity, err := NewEntity("Golang Gopher", "Test Key", "[email protected]", &packet.Config{ RSABits: 1024})
entity, err := NewEntity("Golang Gopher", "Test Key", "[email protected]", &packet.Config{RSABits: 1024})
if err != nil {
t.Fatal(err)
}
Expand All @@ -282,8 +282,7 @@ func TestEncryptWithAEAD(t *testing.T) {
t.Fatal(err)
}

var list []*Entity
list = make([]*Entity, 1)
list := make([]*Entity, 1)
list[0] = entity
entityList := EntityList(list)
buf := bytes.NewBuffer(nil)
Expand All @@ -308,7 +307,7 @@ func TestEncryptWithAEAD(t *testing.T) {
}
dec, err := ioutil.ReadAll(m.decrypted)

if bytes.Compare(dec, []byte(message)) != 0 {
if !bytes.Equal(dec, []byte(message)) {
t.Error("decrypted does not match original")
}
}
Expand All @@ -318,7 +317,7 @@ func TestSignWithHMAC(t *testing.T) {
Algorithm: packet.ExperimentalPubKeyAlgoHMAC,
DefaultHash: crypto.SHA512,
}
entity, err := NewEntity("Golang Gopher", "Test Key", "[email protected]", &packet.Config{ RSABits: 1024})
entity, err := NewEntity("Golang Gopher", "Test Key", "[email protected]", &packet.Config{RSABits: 1024})
if err != nil {
t.Fatal(err)
}
Expand All @@ -327,8 +326,7 @@ func TestSignWithHMAC(t *testing.T) {
if err != nil {
t.Fatal(err)
}
var list []*Entity
list = make([]*Entity, 1)
list := make([]*Entity, 1)
list[0] = entity
entityList := EntityList(list)

Expand Down

0 comments on commit 3f6d02a

Please sign in to comment.