mirror of
https://github.com/emmansun/gmsm.git
synced 2025-04-26 12:16:20 +08:00
Merge pull request #62 from opensvn/main
change the function name and use the correct verb
This commit is contained in:
commit
0ea5fa3966
32
sm9/sm9.go
32
sm9/sm9.go
@ -318,8 +318,8 @@ func (pub *EncryptMasterPublicKey) ScalarBaseMult(r *big.Int) *GT {
|
|||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
// WrappKey generate and wrapp key wtih reciever's uid and system hid
|
// WrapKey generate and wrap key wtih reciever's uid and system hid
|
||||||
func WrappKey(rand io.Reader, pub *EncryptMasterPublicKey, uid []byte, hid byte, kLen int) (key []byte, cipher *G1, err error) {
|
func WrapKey(rand io.Reader, pub *EncryptMasterPublicKey, uid []byte, hid byte, kLen int) (key []byte, cipher *G1, err error) {
|
||||||
q := pub.GenerateUserPublicKey(uid, hid)
|
q := pub.GenerateUserPublicKey(uid, hid)
|
||||||
var r *big.Int
|
var r *big.Int
|
||||||
var ok bool
|
var ok bool
|
||||||
@ -346,9 +346,9 @@ func WrappKey(rand io.Reader, pub *EncryptMasterPublicKey, uid []byte, hid byte,
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// WrappKey wrapp key and marshal the cipher as ASN1 format.
|
// WrapKey wrap key and marshal the cipher as ASN1 format.
|
||||||
func (pub *EncryptMasterPublicKey) WrappKey(rand io.Reader, uid []byte, hid byte, kLen int) ([]byte, []byte, error) {
|
func (pub *EncryptMasterPublicKey) WrapKey(rand io.Reader, uid []byte, hid byte, kLen int) ([]byte, []byte, error) {
|
||||||
key, cipher, err := WrappKey(rand, pub, uid, hid, kLen)
|
key, cipher, err := WrapKey(rand, pub, uid, hid, kLen)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
@ -359,10 +359,10 @@ func (pub *EncryptMasterPublicKey) WrappKey(rand io.Reader, uid []byte, hid byte
|
|||||||
return key, cipherASN1, err
|
return key, cipherASN1, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// WrappKey wrapp key and marshal the result of SM9KeyPackage as ASN1 format. according
|
// WrapKeyASN1 wrap key and marshal the result of SM9KeyPackage as ASN1 format. according
|
||||||
// SM9 cryptographic algorithm application specification
|
// SM9 cryptographic algorithm application specification
|
||||||
func (pub *EncryptMasterPublicKey) WrappKeyASN1(rand io.Reader, uid []byte, hid byte, kLen int) ([]byte, error) {
|
func (pub *EncryptMasterPublicKey) WrapKeyASN1(rand io.Reader, uid []byte, hid byte, kLen int) ([]byte, error) {
|
||||||
key, cipher, err := WrappKey(rand, pub, uid, hid, kLen)
|
key, cipher, err := WrapKey(rand, pub, uid, hid, kLen)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -397,8 +397,8 @@ func UnmarshalSM9KeyPackage(der []byte) ([]byte, *G1, error) {
|
|||||||
return key, g, nil
|
return key, g, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnwrappKey unwrapper key from cipher, user id and aligned key length
|
// UnwrapKey unwrap key from cipher, user id and aligned key length
|
||||||
func UnwrappKey(priv *EncryptPrivateKey, uid []byte, cipher *G1, kLen int) ([]byte, error) {
|
func UnwrapKey(priv *EncryptPrivateKey, uid []byte, cipher *G1, kLen int) ([]byte, error) {
|
||||||
if !cipher.p.IsOnCurve() {
|
if !cipher.p.IsOnCurve() {
|
||||||
return nil, errors.New("sm9: invalid cipher, it's NOT on curve")
|
return nil, errors.New("sm9: invalid cipher, it's NOT on curve")
|
||||||
}
|
}
|
||||||
@ -417,7 +417,7 @@ func UnwrappKey(priv *EncryptPrivateKey, uid []byte, cipher *G1, kLen int) ([]by
|
|||||||
return key, nil
|
return key, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (priv *EncryptPrivateKey) UnwrappKey(uid, cipherDer []byte, kLen int) ([]byte, error) {
|
func (priv *EncryptPrivateKey) UnwrapKey(uid, cipherDer []byte, kLen int) ([]byte, error) {
|
||||||
bytes := make([]byte, 64+1)
|
bytes := make([]byte, 64+1)
|
||||||
input := cryptobyte.String(cipherDer)
|
input := cryptobyte.String(cipherDer)
|
||||||
if !input.ReadASN1BitStringAsBytes(&bytes) || !input.Empty() {
|
if !input.ReadASN1BitStringAsBytes(&bytes) || !input.Empty() {
|
||||||
@ -431,12 +431,12 @@ func (priv *EncryptPrivateKey) UnwrappKey(uid, cipherDer []byte, kLen int) ([]by
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return UnwrappKey(priv, uid, g, kLen)
|
return UnwrapKey(priv, uid, g, kLen)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Encrypt encrypt plaintext, output ciphertext with format C1||C3||C2
|
// Encrypt encrypt plaintext, output ciphertext with format C1||C3||C2
|
||||||
func Encrypt(rand io.Reader, pub *EncryptMasterPublicKey, uid []byte, hid byte, plaintext []byte) ([]byte, error) {
|
func Encrypt(rand io.Reader, pub *EncryptMasterPublicKey, uid []byte, hid byte, plaintext []byte) ([]byte, error) {
|
||||||
key, cipher, err := WrappKey(rand, pub, uid, hid, len(plaintext)+sm3.Size)
|
key, cipher, err := WrapKey(rand, pub, uid, hid, len(plaintext)+sm3.Size)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -460,7 +460,7 @@ func EncryptASN1(rand io.Reader, pub *EncryptMasterPublicKey, uid []byte, hid by
|
|||||||
// Encrypt encrypt plaintext and output ciphertext with ASN.1 format according
|
// Encrypt encrypt plaintext and output ciphertext with ASN.1 format according
|
||||||
// SM9 cryptographic algorithm application specification
|
// SM9 cryptographic algorithm application specification
|
||||||
func (pub *EncryptMasterPublicKey) Encrypt(rand io.Reader, uid []byte, hid byte, plaintext []byte) ([]byte, error) {
|
func (pub *EncryptMasterPublicKey) Encrypt(rand io.Reader, uid []byte, hid byte, plaintext []byte) ([]byte, error) {
|
||||||
key, cipher, err := WrappKey(rand, pub, uid, hid, len(plaintext)+sm3.Size)
|
key, cipher, err := WrapKey(rand, pub, uid, hid, len(plaintext)+sm3.Size)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -488,7 +488,7 @@ func Decrypt(priv *EncryptPrivateKey, uid, ciphertext []byte) ([]byte, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
key, err := UnwrappKey(priv, uid, c, len(c3))
|
key, err := UnwrapKey(priv, uid, c, len(c3))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -543,7 +543,7 @@ func DecryptASN1(priv *EncryptPrivateKey, uid, ciphertext []byte) ([]byte, error
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
key, err := UnwrappKey(priv, uid, c, len(c2Bytes)+len(c3Bytes))
|
key, err := UnwrapKey(priv, uid, c, len(c2Bytes)+len(c3Bytes))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -78,7 +78,7 @@ func TestSignASN1(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWrappKey(t *testing.T) {
|
func TestWrapKey(t *testing.T) {
|
||||||
masterKey, err := GenerateEncryptMasterKey(rand.Reader)
|
masterKey, err := GenerateEncryptMasterKey(rand.Reader)
|
||||||
hid := byte(0x01)
|
hid := byte(0x01)
|
||||||
uid := []byte("emmansun")
|
uid := []byte("emmansun")
|
||||||
@ -89,12 +89,12 @@ func TestWrappKey(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
key, cipher, err := WrappKey(rand.Reader, masterKey.Public(), uid, hid, 16)
|
key, cipher, err := WrapKey(rand.Reader, masterKey.Public(), uid, hid, 16)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
key2, err := UnwrappKey(userKey, uid, cipher, 16)
|
key2, err := UnwrapKey(userKey, uid, cipher, 16)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -104,7 +104,7 @@ func TestWrappKey(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWrappKeyASN1(t *testing.T) {
|
func TestWrapKeyASN1(t *testing.T) {
|
||||||
masterKey, err := GenerateEncryptMasterKey(rand.Reader)
|
masterKey, err := GenerateEncryptMasterKey(rand.Reader)
|
||||||
hid := byte(0x01)
|
hid := byte(0x01)
|
||||||
uid := []byte("emmansun")
|
uid := []byte("emmansun")
|
||||||
@ -115,12 +115,12 @@ func TestWrappKeyASN1(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
key, cipher, err := masterKey.Public().WrappKey(rand.Reader, uid, hid, 16)
|
key, cipher, err := masterKey.Public().WrapKey(rand.Reader, uid, hid, 16)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
key2, err := userKey.UnwrappKey(uid, cipher, 16)
|
key2, err := userKey.UnwrapKey(uid, cipher, 16)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -141,7 +141,7 @@ func TestUnmarshalSM9KeyPackage(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
p, err := masterKey.Public().WrappKeyASN1(rand.Reader, uid, hid, 16)
|
p, err := masterKey.Public().WrapKeyASN1(rand.Reader, uid, hid, 16)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -151,7 +151,7 @@ func TestUnmarshalSM9KeyPackage(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
key2, err := UnwrappKey(userKey, uid, cipher, 16)
|
key2, err := UnwrapKey(userKey, uid, cipher, 16)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -161,7 +161,7 @@ func TestUnmarshalSM9KeyPackage(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWrappKeySM9Sample(t *testing.T) {
|
func TestWrapKeySM9Sample(t *testing.T) {
|
||||||
expectedKey := "4ff5cf86d2ad40c8f4bac98d76abdbde0c0e2f0a829d3f911ef5b2bce0695480"
|
expectedKey := "4ff5cf86d2ad40c8f4bac98d76abdbde0c0e2f0a829d3f911ef5b2bce0695480"
|
||||||
masterKey := new(EncryptMasterPrivateKey)
|
masterKey := new(EncryptMasterPrivateKey)
|
||||||
masterKey.D = bigFromHex("01EDEE3778F441F8DEA3D9FA0ACC4E07EE36C93F9A08618AF4AD85CEDE1C22")
|
masterKey.D = bigFromHex("01EDEE3778F441F8DEA3D9FA0ACC4E07EE36C93F9A08618AF4AD85CEDE1C22")
|
||||||
@ -200,7 +200,7 @@ func TestWrappKeySM9Sample(t *testing.T) {
|
|||||||
t.Errorf("expected %v, got %v\n", expectedKey, hex.EncodeToString(key))
|
t.Errorf("expected %v, got %v\n", expectedKey, hex.EncodeToString(key))
|
||||||
}
|
}
|
||||||
|
|
||||||
key2, err := UnwrappKey(userKey, uid, cipher, 32)
|
key2, err := UnwrapKey(userKey, uid, cipher, 32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user