2020-08-12 14:03:11 +00:00
|
|
|
package signature
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
2022-02-22 11:25:43 +00:00
|
|
|
"fmt"
|
2020-08-12 14:03:11 +00:00
|
|
|
|
2022-02-22 11:25:43 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
2020-08-12 14:03:11 +00:00
|
|
|
crypto "github.com/nspcc-dev/neofs-crypto"
|
|
|
|
)
|
|
|
|
|
|
|
|
type cfg struct {
|
2022-02-22 11:25:43 +00:00
|
|
|
defaultScheme refs.SignatureScheme
|
|
|
|
restrictScheme refs.SignatureScheme
|
2020-08-12 14:03:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func defaultCfg() *cfg {
|
|
|
|
return &cfg{
|
2022-02-22 11:25:43 +00:00
|
|
|
defaultScheme: refs.ECDSA_SHA512,
|
|
|
|
restrictScheme: refs.UnspecifiedScheme,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func verify(cfg *cfg, data []byte, sig *refs.Signature) error {
|
|
|
|
scheme := sig.GetScheme()
|
|
|
|
if scheme == refs.UnspecifiedScheme {
|
|
|
|
scheme = cfg.defaultScheme
|
|
|
|
}
|
|
|
|
if cfg.restrictScheme != refs.UnspecifiedScheme && scheme != cfg.restrictScheme {
|
|
|
|
return fmt.Errorf("%w: unexpected signature scheme", crypto.ErrInvalidSignature)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub := crypto.UnmarshalPublicKey(sig.GetKey())
|
|
|
|
switch scheme {
|
|
|
|
case refs.ECDSA_SHA512:
|
|
|
|
return crypto.Verify(pub, data, sig.GetSign())
|
|
|
|
case refs.ECDSA_RFC6979_SHA256:
|
|
|
|
return crypto.VerifyRFC6979(pub, data, sig.GetSign())
|
|
|
|
default:
|
|
|
|
return crypto.ErrInvalidSignature
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func sign(cfg *cfg, scheme refs.SignatureScheme, key *ecdsa.PrivateKey, data []byte) ([]byte, error) {
|
|
|
|
switch scheme {
|
|
|
|
case refs.ECDSA_SHA512:
|
|
|
|
return crypto.Sign(key, data)
|
|
|
|
case refs.ECDSA_RFC6979_SHA256:
|
|
|
|
return crypto.SignRFC6979(key, data)
|
|
|
|
default:
|
|
|
|
panic("unsupported scheme")
|
2020-08-12 14:03:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func SignWithRFC6979() SignOption {
|
|
|
|
return func(c *cfg) {
|
2022-02-22 11:25:43 +00:00
|
|
|
c.defaultScheme = refs.ECDSA_RFC6979_SHA256
|
|
|
|
c.restrictScheme = refs.ECDSA_RFC6979_SHA256
|
2020-08-12 14:03:11 +00:00
|
|
|
}
|
|
|
|
}
|