2019-05-23 01:31:11 +08:00
|
|
|
package crypto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
pb "github.com/libp2p/go-libp2p-core/crypto/pb"
|
2022-04-19 03:40:37 +08:00
|
|
|
"github.com/libp2p/go-libp2p-core/internal/catch"
|
2019-05-23 01:31:11 +08:00
|
|
|
|
2021-07-23 03:18:09 +08:00
|
|
|
"github.com/btcsuite/btcd/btcec"
|
|
|
|
"github.com/minio/sha256-simd"
|
2019-05-23 01:31:11 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Secp256k1PrivateKey is an Secp256k1 private key
|
|
|
|
type Secp256k1PrivateKey btcec.PrivateKey
|
|
|
|
|
|
|
|
// Secp256k1PublicKey is an Secp256k1 public key
|
|
|
|
type Secp256k1PublicKey btcec.PublicKey
|
|
|
|
|
|
|
|
// GenerateSecp256k1Key generates a new Secp256k1 private and public key pair
|
|
|
|
func GenerateSecp256k1Key(src io.Reader) (PrivKey, PubKey, error) {
|
|
|
|
privk, err := btcec.NewPrivateKey(btcec.S256())
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
k := (*Secp256k1PrivateKey)(privk)
|
|
|
|
return k, k.GetPublic(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalSecp256k1PrivateKey returns a private key from bytes
|
2022-04-19 03:40:37 +08:00
|
|
|
func UnmarshalSecp256k1PrivateKey(data []byte) (k PrivKey, err error) {
|
2019-05-23 01:31:11 +08:00
|
|
|
if len(data) != btcec.PrivKeyBytesLen {
|
|
|
|
return nil, fmt.Errorf("expected secp256k1 data size to be %d", btcec.PrivKeyBytesLen)
|
|
|
|
}
|
2022-04-19 03:40:37 +08:00
|
|
|
defer func() { catch.HandlePanic(recover(), &err, "secp256k1 private-key unmarshal") }()
|
2019-05-23 01:31:11 +08:00
|
|
|
|
|
|
|
privk, _ := btcec.PrivKeyFromBytes(btcec.S256(), data)
|
|
|
|
return (*Secp256k1PrivateKey)(privk), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalSecp256k1PublicKey returns a public key from bytes
|
2022-04-19 03:40:37 +08:00
|
|
|
func UnmarshalSecp256k1PublicKey(data []byte) (_k PubKey, err error) {
|
|
|
|
defer func() { catch.HandlePanic(recover(), &err, "secp256k1 public-key unmarshal") }()
|
2019-05-23 01:31:11 +08:00
|
|
|
k, err := btcec.ParsePubKey(data, btcec.S256())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return (*Secp256k1PublicKey)(k), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type returns the private key type
|
|
|
|
func (k *Secp256k1PrivateKey) Type() pb.KeyType {
|
|
|
|
return pb.KeyType_Secp256k1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Raw returns the bytes of the key
|
|
|
|
func (k *Secp256k1PrivateKey) Raw() ([]byte, error) {
|
|
|
|
return (*btcec.PrivateKey)(k).Serialize(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Equals compares two private keys
|
|
|
|
func (k *Secp256k1PrivateKey) Equals(o Key) bool {
|
|
|
|
sk, ok := o.(*Secp256k1PrivateKey)
|
|
|
|
if !ok {
|
2019-06-28 23:23:16 +08:00
|
|
|
return basicEquals(k, o)
|
2019-05-23 01:31:11 +08:00
|
|
|
}
|
|
|
|
|
2019-06-29 18:08:33 +08:00
|
|
|
return k.GetPublic().Equals(sk.GetPublic())
|
2019-05-23 01:31:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sign returns a signature from input data
|
2022-04-19 03:40:37 +08:00
|
|
|
func (k *Secp256k1PrivateKey) Sign(data []byte) (_sig []byte, err error) {
|
|
|
|
defer func() { catch.HandlePanic(recover(), &err, "secp256k1 signing") }()
|
2019-05-23 01:31:11 +08:00
|
|
|
hash := sha256.Sum256(data)
|
|
|
|
sig, err := (*btcec.PrivateKey)(k).Sign(hash[:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return sig.Serialize(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPublic returns a public key
|
|
|
|
func (k *Secp256k1PrivateKey) GetPublic() PubKey {
|
|
|
|
return (*Secp256k1PublicKey)((*btcec.PrivateKey)(k).PubKey())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type returns the public key type
|
|
|
|
func (k *Secp256k1PublicKey) Type() pb.KeyType {
|
|
|
|
return pb.KeyType_Secp256k1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Raw returns the bytes of the key
|
2022-04-19 03:40:37 +08:00
|
|
|
func (k *Secp256k1PublicKey) Raw() (res []byte, err error) {
|
|
|
|
defer func() { catch.HandlePanic(recover(), &err, "secp256k1 public key marshaling") }()
|
2019-05-23 01:31:11 +08:00
|
|
|
return (*btcec.PublicKey)(k).SerializeCompressed(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Equals compares two public keys
|
|
|
|
func (k *Secp256k1PublicKey) Equals(o Key) bool {
|
|
|
|
sk, ok := o.(*Secp256k1PublicKey)
|
|
|
|
if !ok {
|
2019-06-28 23:23:16 +08:00
|
|
|
return basicEquals(k, o)
|
2019-05-23 01:31:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return (*btcec.PublicKey)(k).IsEqual((*btcec.PublicKey)(sk))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify compares a signature against the input data
|
2022-04-19 03:40:37 +08:00
|
|
|
func (k *Secp256k1PublicKey) Verify(data []byte, sigStr []byte) (success bool, err error) {
|
|
|
|
defer func() {
|
|
|
|
catch.HandlePanic(recover(), &err, "secp256k1 signature verification")
|
|
|
|
|
|
|
|
// To be extra safe.
|
|
|
|
if err != nil {
|
|
|
|
success = false
|
|
|
|
}
|
|
|
|
}()
|
2019-05-23 01:31:11 +08:00
|
|
|
sig, err := btcec.ParseDERSignature(sigStr, btcec.S256())
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
hash := sha256.Sum256(data)
|
|
|
|
return sig.Verify(hash[:], (*btcec.PublicKey)(k)), nil
|
|
|
|
}
|