From 025d072ff1ef2e56bb9890b0bc6bbd6281a20757 Mon Sep 17 00:00:00 2001 From: technicallyty <48813565+technicallyty@users.noreply.github.com> Date: Mon, 10 May 2021 16:06:17 -0700 Subject: [PATCH] Crypto v0.43 Audit updates (#9292) * fix tests * calculate fieldSize for esdca test * remove require declaration for consistency Co-authored-by: technicallyty <48813565+tytech3@users.noreply.github.com> --- crypto/keys/internal/ecdsa/privkey_internal_test.go | 2 +- crypto/keys/internal/ecdsa/pubkey.go | 4 +++- crypto/keys/internal/ecdsa/pubkey_internal_test.go | 6 ++++-- crypto/keys/secp256r1/privkey.go | 3 +++ crypto/keys/secp256r1/privkey_internal_test.go | 4 ++-- crypto/keys/secp256r1/pubkey.go | 3 +++ crypto/keys/secp256r1/pubkey_internal_test.go | 7 +++++++ 7 files changed, 23 insertions(+), 6 deletions(-) diff --git a/crypto/keys/internal/ecdsa/privkey_internal_test.go b/crypto/keys/internal/ecdsa/privkey_internal_test.go index 095e593656..cee214dbd6 100644 --- a/crypto/keys/internal/ecdsa/privkey_internal_test.go +++ b/crypto/keys/internal/ecdsa/privkey_internal_test.go @@ -24,7 +24,7 @@ func (suite *SKSuite) TestPubKey() { suite.True(suite.sk.PublicKey.Equal(&pk.PublicKey)) } -func (suite *SKSuite) Bytes() { +func (suite *SKSuite) TestBytes() { bz := suite.sk.Bytes() suite.Len(bz, 32) var sk *PrivKey diff --git a/crypto/keys/internal/ecdsa/pubkey.go b/crypto/keys/internal/ecdsa/pubkey.go index 4c9b493a6b..7961b93d3e 100644 --- a/crypto/keys/internal/ecdsa/pubkey.go +++ b/crypto/keys/internal/ecdsa/pubkey.go @@ -26,7 +26,9 @@ type PubKey struct { address tmcrypto.Address } -// Address creates an ADR-28 address for ECDSA keys. protoName is a concrete proto structure id. +// Address gets the address associated with a pubkey. If no address exists, it returns a newly created ADR-28 address +// for ECDSA keys. +// protoName is a concrete proto structure id. func (pk *PubKey) Address(protoName string) tmcrypto.Address { if pk.address == nil { pk.address = address.Hash(protoName, pk.Bytes()) diff --git a/crypto/keys/internal/ecdsa/pubkey_internal_test.go b/crypto/keys/internal/ecdsa/pubkey_internal_test.go index 2a46bdff39..88dc95a3bb 100644 --- a/crypto/keys/internal/ecdsa/pubkey_internal_test.go +++ b/crypto/keys/internal/ecdsa/pubkey_internal_test.go @@ -48,9 +48,11 @@ func (suite *PKSuite) TestString() { } func (suite *PKSuite) TestBytes() { - require := suite.Require() + bz := suite.sk.Bytes() + fieldSize := (suite.sk.Curve.Params().BitSize + 7) / 8 + suite.Len(bz, fieldSize) var pk *PubKey - require.Nil(pk.Bytes()) + suite.Nil(pk.Bytes()) } func (suite *PKSuite) TestMarshal() { diff --git a/crypto/keys/secp256r1/privkey.go b/crypto/keys/secp256r1/privkey.go index 8ca725420f..c6702111c7 100644 --- a/crypto/keys/secp256r1/privkey.go +++ b/crypto/keys/secp256r1/privkey.go @@ -33,6 +33,9 @@ func (m *PrivKey) Sign(msg []byte) ([]byte, error) { // Bytes serialize the private key. func (m *PrivKey) Bytes() []byte { + if m == nil { + return nil + } return m.Secret.Bytes() } diff --git a/crypto/keys/secp256r1/privkey_internal_test.go b/crypto/keys/secp256r1/privkey_internal_test.go index d690cbf472..74ad9ec1ac 100644 --- a/crypto/keys/secp256r1/privkey_internal_test.go +++ b/crypto/keys/secp256r1/privkey_internal_test.go @@ -3,7 +3,7 @@ package secp256r1 import ( "testing" - proto "github.com/gogo/protobuf/proto" + "github.com/gogo/protobuf/proto" "github.com/stretchr/testify/suite" "github.com/tendermint/tendermint/crypto" @@ -41,7 +41,7 @@ func (suite *SKSuite) TestPubKey() { suite.True(suite.sk.(*PrivKey).Secret.PublicKey.Equal(&pk.(*PubKey).Key.PublicKey)) } -func (suite *SKSuite) Bytes() { +func (suite *SKSuite) TestBytes() { bz := suite.sk.Bytes() suite.Len(bz, fieldSize) var sk *PrivKey diff --git a/crypto/keys/secp256r1/pubkey.go b/crypto/keys/secp256r1/pubkey.go index d75c0f5298..d462deff89 100644 --- a/crypto/keys/secp256r1/pubkey.go +++ b/crypto/keys/secp256r1/pubkey.go @@ -15,6 +15,9 @@ func (m *PubKey) String() string { // Bytes implements SDK PubKey interface. func (m *PubKey) Bytes() []byte { + if m == nil { + return nil + } return m.Key.Bytes() } diff --git a/crypto/keys/secp256r1/pubkey_internal_test.go b/crypto/keys/secp256r1/pubkey_internal_test.go index e661fccad8..3df25ebedf 100644 --- a/crypto/keys/secp256r1/pubkey_internal_test.go +++ b/crypto/keys/secp256r1/pubkey_internal_test.go @@ -44,6 +44,13 @@ func (suite *PKSuite) TestType() { suite.Require().Equal(name, suite.pk.Type()) } +func (suite *PKSuite) TestBytes() { + bz := suite.pk.Bytes() + suite.Len(bz, fieldSize+1) + var pk *PubKey + suite.Nil(pk.Bytes()) +} + func (suite *PKSuite) TestEquals() { require := suite.Require()