From 3f6d02a4c11f43864892b7b28d372fea30ab71eb Mon Sep 17 00:00:00 2001 From: Lukas Burkhalter Date: Fri, 15 Sep 2023 15:14:04 +0200 Subject: [PATCH] fix: Address warnings --- openpgp/forwarding.go | 4 ++-- openpgp/forwarding_test.go | 17 +++++++------- openpgp/keys_test.go | 47 +++++++++++++++++++------------------- openpgp/read_test.go | 2 +- openpgp/write_test.go | 14 +++++------- 5 files changed, 41 insertions(+), 43 deletions(-) diff --git a/openpgp/forwarding.go b/openpgp/forwarding.go index ae45c3c2..c5622932 100644 --- a/openpgp/forwarding.go +++ b/openpgp/forwarding.go @@ -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 diff --git a/openpgp/forwarding_test.go b/openpgp/forwarding_test.go index c03dd8c5..7bc16718 100644 --- a/openpgp/forwarding_test.go +++ b/openpgp/forwarding_test.go @@ -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----- @@ -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") } } @@ -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) } @@ -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") } @@ -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") } @@ -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") } } diff --git a/openpgp/keys_test.go b/openpgp/keys_test.go index 26b14571..184325a6 100644 --- a/openpgp/keys_test.go +++ b/openpgp/keys_test.go @@ -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{ @@ -1172,7 +1172,7 @@ func TestAddSubkeySerialized(t *testing.T) { func TestAddHMACSubkey(t *testing.T) { c := &packet.Config{ - RSABits: 512, + RSABits: 512, Algorithm: packet.ExperimentalPubKeyAlgoHMAC, } @@ -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) } @@ -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", "no-reply@golang.com", &packet.Config{ RSABits: 1024}) + entity, err := NewEntity("Golang Gopher", "Test Key", "no-reply@golang.com", &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) @@ -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", "no-reply@golang.com", &packet.Config{RSABits: 1024}) @@ -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) } @@ -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", "no-reply@golang.com", &packet.Config{RSABits: 1024}) diff --git a/openpgp/read_test.go b/openpgp/read_test.go index 78baa19c..99c390bd 100644 --- a/openpgp/read_test.go +++ b/openpgp/read_test.go @@ -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) diff --git a/openpgp/write_test.go b/openpgp/write_test.go index 3cd03d85..e2b8acb0 100644 --- a/openpgp/write_test.go +++ b/openpgp/write_test.go @@ -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", "no-reply@golang.com", &packet.Config{ RSABits: 1024}) + entity, err := NewEntity("Golang Gopher", "Test Key", "no-reply@golang.com", &packet.Config{RSABits: 1024}) if err != nil { t.Fatal(err) } @@ -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) @@ -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") } } @@ -318,7 +317,7 @@ func TestSignWithHMAC(t *testing.T) { Algorithm: packet.ExperimentalPubKeyAlgoHMAC, DefaultHash: crypto.SHA512, } - entity, err := NewEntity("Golang Gopher", "Test Key", "no-reply@golang.com", &packet.Config{ RSABits: 1024}) + entity, err := NewEntity("Golang Gopher", "Test Key", "no-reply@golang.com", &packet.Config{RSABits: 1024}) if err != nil { t.Fatal(err) } @@ -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)