2022-04-05 11:13:34 +00:00
|
|
|
package neofsecdsa
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/sha512"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto"
|
|
|
|
)
|
|
|
|
|
2022-04-19 08:11:29 +00:00
|
|
|
// Signer wraps ecdsa.PrivateKey and represents signer based on ECDSA with
|
|
|
|
// SHA-512 hashing. Provides neofscrypto.Signer interface.
|
2022-04-05 11:13:34 +00:00
|
|
|
//
|
2022-04-19 08:11:29 +00:00
|
|
|
// Instances MUST be initialized from ecdsa.PrivateKey using type conversion.
|
|
|
|
type Signer ecdsa.PrivateKey
|
2022-04-05 11:13:34 +00:00
|
|
|
|
2022-04-19 08:11:29 +00:00
|
|
|
// Scheme returns neofscrypto.ECDSA_SHA512.
|
|
|
|
// Implements neofscrypto.Signer.
|
2022-04-05 11:13:34 +00:00
|
|
|
func (x Signer) Scheme() neofscrypto.Scheme {
|
|
|
|
return neofscrypto.ECDSA_SHA512
|
|
|
|
}
|
|
|
|
|
2022-04-19 08:11:29 +00:00
|
|
|
// Sign signs data using ECDSA algorithm with SHA-512 hashing.
|
|
|
|
// Implements neofscrypto.Signer.
|
2022-04-05 11:13:34 +00:00
|
|
|
func (x Signer) Sign(data []byte) ([]byte, error) {
|
|
|
|
h := sha512.Sum512(data)
|
2022-04-19 08:11:29 +00:00
|
|
|
r, s, err := ecdsa.Sign(rand.Reader, (*ecdsa.PrivateKey)(&x), h[:])
|
2022-04-05 11:13:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return elliptic.Marshal(elliptic.P256(), r, s), nil
|
|
|
|
}
|
|
|
|
|
2022-04-19 08:11:29 +00:00
|
|
|
// Public initializes PublicKey and returns it as neofscrypto.PublicKey.
|
|
|
|
// Implements neofscrypto.Signer.
|
2022-04-19 07:39:50 +00:00
|
|
|
func (x Signer) Public() neofscrypto.PublicKey {
|
2022-04-19 08:11:29 +00:00
|
|
|
return (*PublicKey)(&x.PublicKey)
|
|
|
|
}
|
2022-04-05 11:13:34 +00:00
|
|
|
|
2022-04-19 08:11:29 +00:00
|
|
|
// SignerRFC6979 wraps ecdsa.PrivateKey and represents signer based on deterministic
|
|
|
|
// ECDSA with SHA-256 hashing (RFC 6979). Provides neofscrypto.Signer interface.
|
|
|
|
//
|
|
|
|
// Instances SHOULD be initialized from ecdsa.PrivateKey using type conversion.
|
|
|
|
type SignerRFC6979 ecdsa.PrivateKey
|
|
|
|
|
|
|
|
// Scheme returns neofscrypto.ECDSA_DETERMINISTIC_SHA256.
|
|
|
|
// Implements neofscrypto.Signer.
|
|
|
|
func (x SignerRFC6979) Scheme() neofscrypto.Scheme {
|
|
|
|
return neofscrypto.ECDSA_DETERMINISTIC_SHA256
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sign signs data using deterministic ECDSA algorithm with SHA-256 hashing.
|
|
|
|
// Implements neofscrypto.Signer.
|
|
|
|
//
|
|
|
|
// See also RFC 6979.
|
|
|
|
func (x SignerRFC6979) Sign(data []byte) ([]byte, error) {
|
|
|
|
p := keys.PrivateKey{PrivateKey: (ecdsa.PrivateKey)(x)}
|
|
|
|
return p.Sign(data), nil
|
|
|
|
}
|
2022-04-05 11:13:34 +00:00
|
|
|
|
2022-04-19 08:11:29 +00:00
|
|
|
// Public initializes PublicKeyRFC6979 and returns it as neofscrypto.PublicKey.
|
|
|
|
// Implements neofscrypto.Signer.
|
|
|
|
func (x SignerRFC6979) Public() neofscrypto.PublicKey {
|
|
|
|
return (*PublicKeyRFC6979)(&x.PublicKey)
|
2022-04-05 11:13:34 +00:00
|
|
|
}
|