Commit d2271f7a authored by pengjun's avatar pengjun

update sm2

parent d0e5f49f
...@@ -31,7 +31,7 @@ func NewAccount(signType string) (*Account, error) { ...@@ -31,7 +31,7 @@ func NewAccount(signType string) (*Account, error) {
} }
account.Address = addr account.Address = addr
} else if signType == crypto.SM2 { } else if signType == crypto.SM2 {
account.PrivateKey, account.PublicKey = gm.GenetateKey() account.PrivateKey, account.PublicKey = gm.GenerateKey()
addr, err := crypto.PubKeyToAddress(account.PublicKey) addr, err := crypto.PubKeyToAddress(account.PublicKey)
if err != nil { if err != nil {
return nil, err return nil, err
......
...@@ -39,15 +39,19 @@ func TestSign(t *testing.T) { ...@@ -39,15 +39,19 @@ func TestSign(t *testing.T) {
} }
func TestSM2(t *testing.T) { func TestSM2(t *testing.T) {
priv, pub := gm.GenetateKey() priv, pub := gm.GenerateKey()
fmt.Println(priv)
fmt.Println(pub)
fmt.Println(types.ToHex(priv))
fmt.Println(types.ToHex(pub)) fmt.Println(types.ToHex(pub))
msg := []byte("sign test") msg := []byte("sign test")
sig := gm.SM2Sign(msg, priv, nil) sig := gm.SM2Sign(priv, msg,nil)
fmt.Printf("sig = %x\n", sig) fmt.Printf("sig = %x\n", sig)
result := gm.SM2Verify(msg, pub, sig, nil) result := gm.SM2Verify(pub, msg, nil, sig)
assert.Equal(t, true, result) assert.Equal(t, true, result)
} }
......
...@@ -14,8 +14,7 @@ const ( ...@@ -14,8 +14,7 @@ const (
SM2PrivateKeyLength = 32 SM2PrivateKeyLength = 32
) )
var DefaultUID = []byte{0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, var DefaultUID = []byte{0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38}
0x13, 0x23, 0x33, 0x43, 0x53, 0x63, 0x73, 0x83}
func getRandBytes(numBytes int) []byte { func getRandBytes(numBytes int) []byte {
b := make([]byte, numBytes) b := make([]byte, numBytes)
...@@ -26,7 +25,7 @@ func getRandBytes(numBytes int) []byte { ...@@ -26,7 +25,7 @@ func getRandBytes(numBytes int) []byte {
return b return b
} }
func privKeyFromBytes(curve elliptic.Curve, pk []byte) (*sm2.PrivateKey, *sm2.PublicKey) { func PrivKeyFromBytes(curve elliptic.Curve, pk []byte) (*sm2.PrivateKey, *sm2.PublicKey) {
x, y := curve.ScalarBaseMult(pk) x, y := curve.ScalarBaseMult(pk)
priv := &sm2.PrivateKey{ priv := &sm2.PrivateKey{
...@@ -46,12 +45,12 @@ func parsePubKey(pubKeyStr []byte) (key *sm2.PublicKey) { ...@@ -46,12 +45,12 @@ func parsePubKey(pubKeyStr []byte) (key *sm2.PublicKey) {
} }
//SerializePublicKey 公钥序列化 //SerializePublicKey 公钥序列化
func serializePublicKey(p *sm2.PublicKey) []byte { func SerializePublicKey(p *sm2.PublicKey) []byte {
return sm2.Compress(p) return sm2.Compress(p)
} }
//SerializePrivateKey 私钥序列化 //SerializePrivateKey 私钥序列化
func serializePrivateKey(p *sm2.PrivateKey) []byte { func SerializePrivateKey(p *sm2.PrivateKey) []byte {
b := make([]byte, 0, SM2PrivateKeyLength) b := make([]byte, 0, SM2PrivateKeyLength)
return paddedAppend(SM2PrivateKeyLength, b, p.D.Bytes()) return paddedAppend(SM2PrivateKeyLength, b, p.D.Bytes())
} }
...@@ -76,7 +75,7 @@ func canonicalizeInt(val *big.Int) []byte { ...@@ -76,7 +75,7 @@ func canonicalizeInt(val *big.Int) []byte {
return b return b
} }
func serializeSignature(r, s *big.Int) []byte { func SerializeSignature(r, s *big.Int) []byte {
rb := canonicalizeInt(r) rb := canonicalizeInt(r)
sb := canonicalizeInt(s) sb := canonicalizeInt(s)
...@@ -95,7 +94,7 @@ func serializeSignature(r, s *big.Int) []byte { ...@@ -95,7 +94,7 @@ func serializeSignature(r, s *big.Int) []byte {
return b return b
} }
func deserializeSignature(sigStr []byte) (*big.Int, *big.Int, error) { func DeserializeSignature(sigStr []byte) (*big.Int, *big.Int, error) {
sig, err := btcec.ParseDERSignature(sigStr, sm2.P256Sm2()) sig, err := btcec.ParseDERSignature(sigStr, sm2.P256Sm2())
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
...@@ -104,7 +103,7 @@ func deserializeSignature(sigStr []byte) (*big.Int, *big.Int, error) { ...@@ -104,7 +103,7 @@ func deserializeSignature(sigStr []byte) (*big.Int, *big.Int, error) {
return sig.R, sig.S, nil return sig.R, sig.S, nil
} }
func GenetateKey() ([]byte, []byte) { func GenerateKey() ([]byte, []byte) {
privKeyBytes := [SM2PrivateKeyLength]byte{} privKeyBytes := [SM2PrivateKeyLength]byte{}
for { for {
...@@ -115,32 +114,32 @@ func GenetateKey() ([]byte, []byte) { ...@@ -115,32 +114,32 @@ func GenetateKey() ([]byte, []byte) {
copy(privKeyBytes[:], key) copy(privKeyBytes[:], key)
break break
} }
priv, pub := privKeyFromBytes(sm2.P256Sm2(), privKeyBytes[:]) priv, pub := PrivKeyFromBytes(sm2.P256Sm2(), privKeyBytes[:])
return serializePrivateKey(priv), serializePublicKey(pub) return SerializePrivateKey(priv), SerializePublicKey(pub)
} }
func SM2Sign(msg []byte, privateKey []byte, uid []byte) []byte { func SM2Sign(privateKey []byte, msg []byte, uid []byte) []byte {
if uid == nil { if uid == nil {
uid = DefaultUID uid = DefaultUID
} }
priv, _ := privKeyFromBytes(sm2.P256Sm2(), privateKey) priv, _ := PrivKeyFromBytes(sm2.P256Sm2(), privateKey)
r, s, err := sm2.Sm2Sign(priv, msg, uid) r, s, err := sm2.Sm2Sign(priv, msg, uid)
if err != nil { if err != nil {
return nil return nil
} }
return serializeSignature(r, s) return SerializeSignature(r, s)
} }
func SM2Verify(msg []byte, publicKey []byte, sig []byte, uid []byte) bool { func SM2Verify(publicKey []byte, msg []byte, uid []byte, sig []byte,) bool {
if uid == nil { if uid == nil {
uid = DefaultUID uid = DefaultUID
} }
pub := parsePubKey(publicKey[:]) pub := parsePubKey(publicKey[:])
r, s, err := deserializeSignature(sig) r, s, err := DeserializeSignature(sig)
if err != nil { if err != nil {
fmt.Errorf("unmarshal sign failed") fmt.Errorf("unmarshal sign failed")
return false return false
...@@ -156,12 +155,12 @@ func SM2Encrypt(publicKey []byte, data []byte) ([]byte, error) { ...@@ -156,12 +155,12 @@ func SM2Encrypt(publicKey []byte, data []byte) ([]byte, error) {
} }
func SM2Decrypt(privateKey []byte, data []byte) ([]byte, error) { func SM2Decrypt(privateKey []byte, data []byte) ([]byte, error) {
priv, _ := privKeyFromBytes(sm2.P256Sm2(), privateKey) priv, _ := PrivKeyFromBytes(sm2.P256Sm2(), privateKey)
return sm2.Decrypt(priv, data) return sm2.Decrypt(priv, data)
} }
func PubKeyFromPrivate(privKey []byte) []byte { func PubKeyFromPrivate(privKey []byte) []byte {
_, pub := privKeyFromBytes(sm2.P256Sm2(), privKey) _, pub := PrivKeyFromBytes(sm2.P256Sm2(), privKey)
return serializePublicKey(pub) return SerializePublicKey(pub)
} }
\ No newline at end of file
...@@ -26,7 +26,7 @@ require ( ...@@ -26,7 +26,7 @@ require (
github.com/spf13/cobra v1.0.0 // indirect github.com/spf13/cobra v1.0.0 // indirect
github.com/stretchr/testify v1.5.1 github.com/stretchr/testify v1.5.1
github.com/syndtr/goleveldb v1.0.0 // indirect github.com/syndtr/goleveldb v1.0.0 // indirect
github.com/tjfoc/gmsm v1.3.1 github.com/tjfoc/gmsm v1.3.2
golang.org/x/crypto v0.0.0-20191219195013-becbf705a915 golang.org/x/crypto v0.0.0-20191219195013-becbf705a915
google.golang.org/grpc v1.29.1 // indirect google.golang.org/grpc v1.29.1 // indirect
) )
...@@ -256,6 +256,8 @@ github.com/syndtr/goleveldb v1.0.0 h1:fBdIW9lB4Iz0n9khmH8w27SJ3QEJ7+IgjPEwGSZiFd ...@@ -256,6 +256,8 @@ github.com/syndtr/goleveldb v1.0.0 h1:fBdIW9lB4Iz0n9khmH8w27SJ3QEJ7+IgjPEwGSZiFd
github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ= github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ=
github.com/tjfoc/gmsm v1.3.1 h1:+k3IAlF81c31/TllJmIfuCYnjl8ziMdTWGWJcP9J1uo= github.com/tjfoc/gmsm v1.3.1 h1:+k3IAlF81c31/TllJmIfuCYnjl8ziMdTWGWJcP9J1uo=
github.com/tjfoc/gmsm v1.3.1/go.mod h1:HaUcFuY0auTiaHB9MHFGCPx5IaLhTUd2atbCFBQXn9w= github.com/tjfoc/gmsm v1.3.1/go.mod h1:HaUcFuY0auTiaHB9MHFGCPx5IaLhTUd2atbCFBQXn9w=
github.com/tjfoc/gmsm v1.3.2 h1:7JVkAn5bvUJ7HtU08iW6UiD+UTmJTIToHCfeFzkcCxM=
github.com/tjfoc/gmsm v1.3.2/go.mod h1:HaUcFuY0auTiaHB9MHFGCPx5IaLhTUd2atbCFBQXn9w=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc= github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment