2021-10-27 10:00:11 +00:00
|
|
|
package signature
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/sha256"
|
|
|
|
"crypto/sha512"
|
2022-02-24 11:45:52 +00:00
|
|
|
"fmt"
|
2021-10-27 10:00:11 +00:00
|
|
|
"math/big"
|
|
|
|
|
2021-12-10 13:56:04 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2022-02-24 11:05:05 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/signature"
|
2021-10-27 10:00:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var curve = elliptic.P256()
|
|
|
|
|
|
|
|
type cfg struct {
|
2022-03-02 10:41:41 +00:00
|
|
|
schemeFixed bool
|
|
|
|
scheme signature.Scheme
|
2021-10-27 10:00:11 +00:00
|
|
|
}
|
|
|
|
|
2022-02-24 11:05:05 +00:00
|
|
|
func getConfig(opts ...SignOption) *cfg {
|
|
|
|
cfg := &cfg{
|
2022-03-02 10:41:41 +00:00
|
|
|
scheme: signature.ECDSAWithSHA512,
|
2021-10-27 10:00:11 +00:00
|
|
|
}
|
2022-02-24 11:05:05 +00:00
|
|
|
|
|
|
|
for i := range opts {
|
|
|
|
opts[i](cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cfg
|
2021-10-27 10:00:11 +00:00
|
|
|
}
|
|
|
|
|
2022-02-24 11:05:05 +00:00
|
|
|
func sign(scheme signature.Scheme, key *ecdsa.PrivateKey, msg []byte) ([]byte, error) {
|
|
|
|
switch scheme {
|
|
|
|
case signature.ECDSAWithSHA512:
|
|
|
|
h := sha512.Sum512(msg)
|
|
|
|
x, y, err := ecdsa.Sign(rand.Reader, key, h[:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return elliptic.Marshal(elliptic.P256(), x, y), nil
|
|
|
|
case signature.RFC6979WithSHA256:
|
|
|
|
p := &keys.PrivateKey{PrivateKey: *key}
|
|
|
|
return p.Sign(msg), nil
|
|
|
|
default:
|
2022-03-02 10:41:41 +00:00
|
|
|
panic(fmt.Sprintf("unsupported scheme %s", scheme))
|
2021-10-27 10:00:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-02 11:41:37 +00:00
|
|
|
func verify(cfg *cfg, msg []byte, f func() (key, sign []byte, scheme signature.Scheme)) error {
|
|
|
|
key, sign, scheme := f()
|
|
|
|
|
|
|
|
pub, err := keys.NewPublicKeyFromBytes(key, elliptic.P256())
|
2022-02-24 11:45:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%w: %v", ErrInvalidPublicKey, err)
|
|
|
|
}
|
|
|
|
|
2022-03-02 10:41:41 +00:00
|
|
|
if !cfg.schemeFixed {
|
2022-03-02 11:41:37 +00:00
|
|
|
cfg.scheme = scheme
|
2022-02-24 11:45:52 +00:00
|
|
|
}
|
|
|
|
|
2022-03-02 10:41:41 +00:00
|
|
|
switch cfg.scheme {
|
2022-02-24 11:05:05 +00:00
|
|
|
case signature.ECDSAWithSHA512:
|
|
|
|
h := sha512.Sum512(msg)
|
2022-03-02 11:41:37 +00:00
|
|
|
r, s := unmarshalXY(sign)
|
2022-02-24 11:45:52 +00:00
|
|
|
if r != nil && s != nil && ecdsa.Verify((*ecdsa.PublicKey)(pub), h[:], r, s) {
|
2022-02-24 11:05:05 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return ErrInvalidSignature
|
|
|
|
case signature.RFC6979WithSHA256:
|
|
|
|
h := sha256.Sum256(msg)
|
2022-03-02 11:41:37 +00:00
|
|
|
if pub.Verify(sign, h[:]) {
|
2022-02-24 11:05:05 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return ErrInvalidSignature
|
|
|
|
default:
|
2022-03-02 10:41:41 +00:00
|
|
|
return fmt.Errorf("unsupported signature scheme %s", cfg.scheme)
|
2021-10-27 10:00:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// unmarshalXY converts a point, serialized by Marshal, into an x, y pair.
|
|
|
|
// It is an error if the point is not in uncompressed form.
|
|
|
|
// On error, x,y = nil.
|
|
|
|
// Unlike the original version of the code, we ignore that x or y not on the curve
|
|
|
|
// --------------
|
|
|
|
// It's copy-paste elliptic.Unmarshal(curve, data) stdlib function, without last line
|
|
|
|
// of code.
|
|
|
|
// Link - https://golang.org/pkg/crypto/elliptic/#Unmarshal
|
|
|
|
func unmarshalXY(data []byte) (x *big.Int, y *big.Int) {
|
|
|
|
if len(data) != PublicKeyUncompressedSize {
|
|
|
|
return
|
|
|
|
} else if data[0] != 4 { // uncompressed form
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p := curve.Params().P
|
|
|
|
x = new(big.Int).SetBytes(data[1:PublicKeyCompressedSize])
|
|
|
|
y = new(big.Int).SetBytes(data[PublicKeyCompressedSize:])
|
|
|
|
|
|
|
|
if x.Cmp(p) >= 0 || y.Cmp(p) >= 0 {
|
|
|
|
x, y = nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func SignWithRFC6979() SignOption {
|
|
|
|
return func(c *cfg) {
|
2022-03-02 10:41:41 +00:00
|
|
|
c.schemeFixed = true
|
|
|
|
c.scheme = signature.RFC6979WithSHA256
|
2021-10-27 10:00:11 +00:00
|
|
|
}
|
|
|
|
}
|