mirror of
https://github.com/emmansun/gmsm.git
synced 2025-04-26 12:16:20 +08:00
sm9: marshal compressed
This commit is contained in:
parent
f437855de0
commit
05aeed5c34
@ -51,6 +51,7 @@ type combinedMult interface {
|
||||
}
|
||||
|
||||
// PrivateKey represents an ECDSA SM2 private key.
|
||||
// It implemented both crypto.Decrypter and crypto.Signer interfaces.
|
||||
type PrivateKey struct {
|
||||
ecdsa.PrivateKey
|
||||
}
|
||||
|
@ -153,6 +153,14 @@ func (pub *SignMasterPublicKey) MarshalASN1() ([]byte, error) {
|
||||
return b.Bytes()
|
||||
}
|
||||
|
||||
// MarshalCompressedASN1 marshal sign master public key to asn.1 format data according
|
||||
// SM9 cryptographic algorithm application specification, the curve point is in compressed form.
|
||||
func (pub *SignMasterPublicKey) MarshalCompressedASN1() ([]byte, error) {
|
||||
var b cryptobyte.Builder
|
||||
b.AddASN1BitString(pub.MasterPublicKey.MarshalCompressed())
|
||||
return b.Bytes()
|
||||
}
|
||||
|
||||
func unmarshalG2(bytes []byte) (*bn256.G2, error) {
|
||||
g2 := new(bn256.G2)
|
||||
switch bytes[0] {
|
||||
@ -207,6 +215,14 @@ func (priv *SignPrivateKey) MarshalASN1() ([]byte, error) {
|
||||
return b.Bytes()
|
||||
}
|
||||
|
||||
// MarshalCompressedASN1 marshal sign user private key to asn.1 format data according
|
||||
// SM9 cryptographic algorithm application specification, the curve point is in compressed form.
|
||||
func (priv *SignPrivateKey) MarshalCompressedASN1() ([]byte, error) {
|
||||
var b cryptobyte.Builder
|
||||
b.AddASN1BitString(priv.PrivateKey.MarshalCompressed())
|
||||
return b.Bytes()
|
||||
}
|
||||
|
||||
func unmarshalG1(bytes []byte) (*bn256.G1, error) {
|
||||
g := new(bn256.G1)
|
||||
switch bytes[0] {
|
||||
@ -343,6 +359,14 @@ func (pub *EncryptMasterPublicKey) MarshalASN1() ([]byte, error) {
|
||||
return b.Bytes()
|
||||
}
|
||||
|
||||
// MarshalCompressedASN1 marshal encrypt master public key to asn.1 format data according
|
||||
// SM9 cryptographic algorithm application specification, the curve point is in compressed form.
|
||||
func (pub *EncryptMasterPublicKey) MarshalCompressedASN1() ([]byte, error) {
|
||||
var b cryptobyte.Builder
|
||||
b.AddASN1BitString(pub.MasterPublicKey.MarshalCompressed())
|
||||
return b.Bytes()
|
||||
}
|
||||
|
||||
// UnmarshalASN1 unmarsal der data to encrypt master public key
|
||||
func (pub *EncryptMasterPublicKey) UnmarshalASN1(der []byte) error {
|
||||
var bytes []byte
|
||||
@ -378,6 +402,14 @@ func (priv *EncryptPrivateKey) MarshalASN1() ([]byte, error) {
|
||||
return b.Bytes()
|
||||
}
|
||||
|
||||
// MarshalCompressedASN1 marshal encrypt user private key to asn.1 format data according
|
||||
// SM9 cryptographic algorithm application specification, the curve point is in compressed form.
|
||||
func (priv *EncryptPrivateKey) MarshalCompressedASN1() ([]byte, error) {
|
||||
var b cryptobyte.Builder
|
||||
b.AddASN1BitString(priv.PrivateKey.MarshalCompressed())
|
||||
return b.Bytes()
|
||||
}
|
||||
|
||||
// UnmarshalASN1 unmarsal der data to encrypt user private key
|
||||
// Note, priv's EncryptMasterPublicKey should be handled separately.
|
||||
func (priv *EncryptPrivateKey) UnmarshalASN1(der []byte) error {
|
||||
|
@ -44,6 +44,25 @@ func TestSignMasterPublicKeyMarshalASN1(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSignMasterPublicKeyMarshalCompressedASN1(t *testing.T) {
|
||||
masterKey, err := GenerateSignMasterKey(rand.Reader)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
der, err := masterKey.Public().MarshalCompressedASN1()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pub2 := new(SignMasterPublicKey)
|
||||
err = pub2.UnmarshalASN1(der)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !masterKey.MasterPublicKey.Equal(pub2.MasterPublicKey) {
|
||||
t.Errorf("not same")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSignUserPrivateKeyMarshalASN1(t *testing.T) {
|
||||
masterKey, err := GenerateSignMasterKey(rand.Reader)
|
||||
uid := []byte("emmansun")
|
||||
@ -69,6 +88,31 @@ func TestSignUserPrivateKeyMarshalASN1(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSignUserPrivateKeyMarshalCompressedASN1(t *testing.T) {
|
||||
masterKey, err := GenerateSignMasterKey(rand.Reader)
|
||||
uid := []byte("emmansun")
|
||||
hid := byte(0x01)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
userKey, err := masterKey.GenerateUserKey(uid, hid)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
der, err := userKey.MarshalCompressedASN1()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
userKey2 := new(SignPrivateKey)
|
||||
err = userKey2.UnmarshalASN1(der)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !userKey.PrivateKey.Equal(userKey2.PrivateKey) {
|
||||
t.Errorf("not same")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncryptMasterPrivateKeyMarshalASN1(t *testing.T) {
|
||||
masterKey, err := GenerateEncryptMasterKey(rand.Reader)
|
||||
if err != nil {
|
||||
@ -107,6 +151,25 @@ func TestEncryptMasterPublicKeyMarshalASN1(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncryptMasterPublicKeyMarshalCompressedASN1(t *testing.T) {
|
||||
masterKey, err := GenerateEncryptMasterKey(rand.Reader)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
der, err := masterKey.Public().MarshalCompressedASN1()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pub2 := new(EncryptMasterPublicKey)
|
||||
err = pub2.UnmarshalASN1(der)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !masterKey.MasterPublicKey.Equal(pub2.MasterPublicKey) {
|
||||
t.Errorf("not same")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncryptUserPrivateKeyMarshalASN1(t *testing.T) {
|
||||
masterKey, err := GenerateEncryptMasterKey(rand.Reader)
|
||||
uid := []byte("emmansun")
|
||||
@ -132,6 +195,31 @@ func TestEncryptUserPrivateKeyMarshalASN1(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncryptUserPrivateKeyMarshalCompressedASN1(t *testing.T) {
|
||||
masterKey, err := GenerateEncryptMasterKey(rand.Reader)
|
||||
uid := []byte("emmansun")
|
||||
hid := byte(0x01)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
userKey, err := masterKey.GenerateUserKey(uid, hid)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
der, err := userKey.MarshalCompressedASN1()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
userKey2 := new(EncryptPrivateKey)
|
||||
err = userKey2.UnmarshalASN1(der)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !userKey.PrivateKey.Equal(userKey2.PrivateKey) {
|
||||
t.Errorf("not same")
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkGenerateSignPrivKey(b *testing.B) {
|
||||
masterKey, err := GenerateSignMasterKey(rand.Reader)
|
||||
uid := []byte("emmansun")
|
||||
|
Loading…
x
Reference in New Issue
Block a user