sm9: marshal compressed

This commit is contained in:
Sun Yimin 2022-08-09 10:01:34 +08:00 committed by GitHub
parent f437855de0
commit 05aeed5c34
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 121 additions and 0 deletions

View File

@ -51,6 +51,7 @@ type combinedMult interface {
} }
// PrivateKey represents an ECDSA SM2 private key. // PrivateKey represents an ECDSA SM2 private key.
// It implemented both crypto.Decrypter and crypto.Signer interfaces.
type PrivateKey struct { type PrivateKey struct {
ecdsa.PrivateKey ecdsa.PrivateKey
} }

View File

@ -153,6 +153,14 @@ func (pub *SignMasterPublicKey) MarshalASN1() ([]byte, error) {
return b.Bytes() 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) { func unmarshalG2(bytes []byte) (*bn256.G2, error) {
g2 := new(bn256.G2) g2 := new(bn256.G2)
switch bytes[0] { switch bytes[0] {
@ -207,6 +215,14 @@ func (priv *SignPrivateKey) MarshalASN1() ([]byte, error) {
return b.Bytes() 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) { func unmarshalG1(bytes []byte) (*bn256.G1, error) {
g := new(bn256.G1) g := new(bn256.G1)
switch bytes[0] { switch bytes[0] {
@ -343,6 +359,14 @@ func (pub *EncryptMasterPublicKey) MarshalASN1() ([]byte, error) {
return b.Bytes() 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 // UnmarshalASN1 unmarsal der data to encrypt master public key
func (pub *EncryptMasterPublicKey) UnmarshalASN1(der []byte) error { func (pub *EncryptMasterPublicKey) UnmarshalASN1(der []byte) error {
var bytes []byte var bytes []byte
@ -378,6 +402,14 @@ func (priv *EncryptPrivateKey) MarshalASN1() ([]byte, error) {
return b.Bytes() 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 // UnmarshalASN1 unmarsal der data to encrypt user private key
// Note, priv's EncryptMasterPublicKey should be handled separately. // Note, priv's EncryptMasterPublicKey should be handled separately.
func (priv *EncryptPrivateKey) UnmarshalASN1(der []byte) error { func (priv *EncryptPrivateKey) UnmarshalASN1(der []byte) error {

View File

@ -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) { func TestSignUserPrivateKeyMarshalASN1(t *testing.T) {
masterKey, err := GenerateSignMasterKey(rand.Reader) masterKey, err := GenerateSignMasterKey(rand.Reader)
uid := []byte("emmansun") 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) { func TestEncryptMasterPrivateKeyMarshalASN1(t *testing.T) {
masterKey, err := GenerateEncryptMasterKey(rand.Reader) masterKey, err := GenerateEncryptMasterKey(rand.Reader)
if err != nil { 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) { func TestEncryptUserPrivateKeyMarshalASN1(t *testing.T) {
masterKey, err := GenerateEncryptMasterKey(rand.Reader) masterKey, err := GenerateEncryptMasterKey(rand.Reader)
uid := []byte("emmansun") 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) { func BenchmarkGenerateSignPrivKey(b *testing.B) {
masterKey, err := GenerateSignMasterKey(rand.Reader) masterKey, err := GenerateSignMasterKey(rand.Reader)
uid := []byte("emmansun") uid := []byte("emmansun")