2021-10-27 10:00:11 +00:00
|
|
|
package signature
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
|
|
|
"errors"
|
2021-12-10 13:56:04 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2021-10-27 10:00:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type DataSource interface {
|
|
|
|
ReadSignedData([]byte) ([]byte, error)
|
|
|
|
SignedDataSize() int
|
|
|
|
}
|
|
|
|
|
|
|
|
type DataWithSignature interface {
|
|
|
|
DataSource
|
|
|
|
GetSignatureWithKey() (key, sig []byte)
|
|
|
|
SetSignatureWithKey(key, sig []byte)
|
|
|
|
}
|
|
|
|
|
|
|
|
type SignOption func(*cfg)
|
|
|
|
|
|
|
|
type KeySignatureHandler func(key []byte, sig []byte)
|
|
|
|
|
|
|
|
type KeySignatureSource func() (key, sig []byte)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// PrivateKeyCompressedSize is constant with compressed size of private key (SK).
|
|
|
|
// D coordinate stored, recover PK by formula x, y = curve.ScalarBaseMul(d,bytes).
|
|
|
|
PrivateKeyCompressedSize = 32
|
|
|
|
|
|
|
|
// PublicKeyCompressedSize is constant with compressed size of public key (PK).
|
|
|
|
PublicKeyCompressedSize = 33
|
|
|
|
|
|
|
|
// PublicKeyUncompressedSize is constant with uncompressed size of public key (PK).
|
|
|
|
// First byte always should be 0x4 other 64 bytes is X and Y (32 bytes per coordinate).
|
|
|
|
// 2 * 32 + 1.
|
|
|
|
PublicKeyUncompressedSize = 65
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrEmptyPrivateKey is returned when used private key is empty.
|
|
|
|
ErrEmptyPrivateKey = errors.New("empty private key")
|
|
|
|
// ErrInvalidPublicKey is returned when public key cannot be unmarshalled.
|
|
|
|
ErrInvalidPublicKey = errors.New("invalid public key")
|
|
|
|
// ErrInvalidSignature is returned if signature cannot be verified.
|
|
|
|
ErrInvalidSignature = errors.New("invalid signature")
|
|
|
|
)
|
|
|
|
|
|
|
|
func DataSignature(key *ecdsa.PrivateKey, src DataSource, opts ...SignOption) ([]byte, error) {
|
|
|
|
if key == nil {
|
|
|
|
return nil, ErrEmptyPrivateKey
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := dataForSignature(src)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer bytesPool.Put(&data)
|
|
|
|
|
|
|
|
cfg := defaultCfg()
|
|
|
|
|
|
|
|
for i := range opts {
|
|
|
|
opts[i](cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cfg.signFunc(key, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func SignDataWithHandler(key *ecdsa.PrivateKey, src DataSource, handler KeySignatureHandler, opts ...SignOption) error {
|
|
|
|
sig, err := DataSignature(key, src, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-10 13:56:04 +00:00
|
|
|
pub := (*keys.PublicKey)(&key.PublicKey)
|
|
|
|
handler(pub.Bytes(), sig)
|
2021-10-27 10:00:11 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func VerifyDataWithSource(dataSrc DataSource, sigSrc KeySignatureSource, opts ...SignOption) error {
|
|
|
|
data, err := dataForSignature(dataSrc)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer bytesPool.Put(&data)
|
|
|
|
|
|
|
|
cfg := defaultCfg()
|
|
|
|
|
|
|
|
for i := range opts {
|
|
|
|
opts[i](cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
key, sig := sigSrc()
|
|
|
|
|
2021-12-10 13:56:04 +00:00
|
|
|
var pub *keys.PublicKey
|
2021-10-27 10:00:11 +00:00
|
|
|
if len(key) != 0 {
|
2021-12-10 13:56:04 +00:00
|
|
|
pub, err = keys.NewPublicKeyFromBytes(key, elliptic.P256())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%w: %v", ErrInvalidPublicKey, err)
|
2021-10-27 10:00:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-10 13:56:04 +00:00
|
|
|
return cfg.verifyFunc(
|
|
|
|
(*ecdsa.PublicKey)(pub),
|
|
|
|
data,
|
|
|
|
sig,
|
|
|
|
)
|
2021-10-27 10:00:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func SignData(key *ecdsa.PrivateKey, v DataWithSignature, opts ...SignOption) error {
|
|
|
|
return SignDataWithHandler(key, v, v.SetSignatureWithKey, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func VerifyData(src DataWithSignature, opts ...SignOption) error {
|
|
|
|
return VerifyDataWithSource(src, src.GetSignatureWithKey, opts...)
|
|
|
|
}
|