Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto: drop neofs-crypto dependency #98

Merged
merged 1 commit into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions crypto/ecdsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/sha256"
"errors"
"io"
"math/big"

crypto "github.com/nspcc-dev/neofs-crypto"
"github.com/nspcc-dev/rfc6979"
)

type (
Expand Down Expand Up @@ -45,25 +47,28 @@
}

// Sign signs message using P-256 curve.
func (e ECDSAPriv) Sign(msg []byte) (sig []byte, err error) {
sig, err = crypto.Sign(e.PrivateKey, msg)
if err != nil {
return nil, err
}
func (e ECDSAPriv) Sign(msg []byte) ([]byte, error) {
h := sha256.Sum256(msg)
r, s := rfc6979.SignECDSA(e.PrivateKey, h[:], sha256.New)

sig := make([]byte, 32*2)
_ = r.FillBytes(sig[:32])
_ = s.FillBytes(sig[32:])

// we chomp first 0x04 (uncompressed) byte
return sig[1:], err
return sig, nil
}

// MarshalBinary implements encoding.BinaryMarshaler interface.
func (e ECDSAPub) MarshalBinary() ([]byte, error) {
return crypto.MarshalPublicKey(e.PublicKey), nil
return elliptic.MarshalCompressed(e.PublicKey.Curve, e.PublicKey.X, e.PublicKey.Y), nil
}

// UnmarshalBinary implements encoding.BinaryUnmarshaler interface.
func (e *ECDSAPub) UnmarshalBinary(data []byte) error {
e.PublicKey = crypto.UnmarshalPublicKey(data)
if e.PublicKey == nil {
e.PublicKey = new(ecdsa.PublicKey)
e.PublicKey.Curve = elliptic.P256()
e.PublicKey.X, e.PublicKey.Y = elliptic.UnmarshalCompressed(e.PublicKey.Curve, data)
if e.PublicKey.X == nil {
return errors.New("can't unmarshal ECDSA public key")
}

Expand All @@ -72,5 +77,12 @@

// Verify verifies signature using P-256 curve.
func (e ECDSAPub) Verify(msg, sig []byte) error {
return crypto.Verify(e.PublicKey, msg, append([]byte{0x04}, sig...))
h := sha256.Sum256(msg)
rBytes := new(big.Int).SetBytes(sig[0:32])
sBytes := new(big.Int).SetBytes(sig[32:64])
res := ecdsa.Verify(e.PublicKey, h[:], rBytes, sBytes)
if !res {
return errors.New("bad signature")

Check warning on line 85 in crypto/ecdsa.go

View check run for this annotation

Codecov / codecov/patch

crypto/ecdsa.go#L85

Added line #L85 was not covered by tests
}
return nil
}
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/nspcc-dev/dbft
go 1.19

require (
github.com/nspcc-dev/neofs-crypto v0.4.0
github.com/nspcc-dev/rfc6979 v0.2.0
github.com/pkg/errors v0.8.1
github.com/spaolacci/murmur3 v1.1.0
github.com/stretchr/testify v1.7.0
Expand All @@ -13,8 +13,6 @@ require (

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/mr-tron/base58 v1.2.0 // indirect
github.com/nspcc-dev/rfc6979 v0.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.uber.org/atomic v1.4.0 // indirect
go.uber.org/multierr v1.1.0 // indirect
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/mr-tron/base58 v1.2.0 h1:T/HDJBh4ZCPbU39/+c3rRvE0uKBQlU27+QI8LJ4t64o=
github.com/mr-tron/base58 v1.2.0/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc=
github.com/nspcc-dev/neofs-crypto v0.4.0 h1:5LlrUAM5O0k1+sH/sktBtrgfWtq1pgpDs09fZo+KYi4=
github.com/nspcc-dev/neofs-crypto v0.4.0/go.mod h1:6XJ8kbXgOfevbI2WMruOtI+qUJXNwSGM/E9eClXxPHs=
github.com/nspcc-dev/rfc6979 v0.2.0 h1:3e1WNxrN60/6N0DW7+UYisLeZJyfqZTNOjeV/toYvOE=
github.com/nspcc-dev/rfc6979 v0.2.0/go.mod h1:exhIh1PdpDC5vQmyEsGvc4YDM/lyQp/452QxGq/UEso=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
Expand Down
Loading