From e5150e19b57fb744f84ed3e16f6d0f9c4f39881d Mon Sep 17 00:00:00 2001 From: opensvn Date: Wed, 15 Jun 2022 13:40:30 +0800 Subject: [PATCH] change the function name and use the correct verb --- sm9/sm9.go | 32 ++++++++++++++++---------------- sm9/sm9_test.go | 20 ++++++++++---------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/sm9/sm9.go b/sm9/sm9.go index 38954f0..773784f 100644 --- a/sm9/sm9.go +++ b/sm9/sm9.go @@ -318,8 +318,8 @@ func (pub *EncryptMasterPublicKey) ScalarBaseMult(r *big.Int) *GT { return e } -// WrappKey generate and wrapp 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) { +// WrapKey generate and wrap key wtih reciever's uid and system hid +func WrapKey(rand io.Reader, pub *EncryptMasterPublicKey, uid []byte, hid byte, kLen int) (key []byte, cipher *G1, err error) { q := pub.GenerateUserPublicKey(uid, hid) var r *big.Int var ok bool @@ -346,9 +346,9 @@ func WrappKey(rand io.Reader, pub *EncryptMasterPublicKey, uid []byte, hid byte, return } -// WrappKey wrapp key and marshal the cipher as ASN1 format. -func (pub *EncryptMasterPublicKey) WrappKey(rand io.Reader, uid []byte, hid byte, kLen int) ([]byte, []byte, error) { - key, cipher, err := WrappKey(rand, pub, uid, hid, kLen) +// WrapKey wrap key and marshal the cipher as ASN1 format. +func (pub *EncryptMasterPublicKey) WrapKey(rand io.Reader, uid []byte, hid byte, kLen int) ([]byte, []byte, error) { + key, cipher, err := WrapKey(rand, pub, uid, hid, kLen) if err != nil { return nil, nil, err } @@ -359,10 +359,10 @@ func (pub *EncryptMasterPublicKey) WrappKey(rand io.Reader, uid []byte, hid byte 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 -func (pub *EncryptMasterPublicKey) WrappKeyASN1(rand io.Reader, uid []byte, hid byte, kLen int) ([]byte, error) { - key, cipher, err := WrappKey(rand, pub, uid, hid, kLen) +func (pub *EncryptMasterPublicKey) WrapKeyASN1(rand io.Reader, uid []byte, hid byte, kLen int) ([]byte, error) { + key, cipher, err := WrapKey(rand, pub, uid, hid, kLen) if err != nil { return nil, err } @@ -397,8 +397,8 @@ func UnmarshalSM9KeyPackage(der []byte) ([]byte, *G1, error) { return key, g, nil } -// UnwrappKey unwrapper key from cipher, user id and aligned key length -func UnwrappKey(priv *EncryptPrivateKey, uid []byte, cipher *G1, kLen int) ([]byte, error) { +// UnwrapKey unwrap key from cipher, user id and aligned key length +func UnwrapKey(priv *EncryptPrivateKey, uid []byte, cipher *G1, kLen int) ([]byte, error) { if !cipher.p.IsOnCurve() { 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 } -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) input := cryptobyte.String(cipherDer) if !input.ReadASN1BitStringAsBytes(&bytes) || !input.Empty() { @@ -431,12 +431,12 @@ func (priv *EncryptPrivateKey) UnwrappKey(uid, cipherDer []byte, kLen int) ([]by if err != nil { 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 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 { 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 // SM9 cryptographic algorithm application specification 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 { return nil, err } @@ -488,7 +488,7 @@ func Decrypt(priv *EncryptPrivateKey, uid, ciphertext []byte) ([]byte, error) { return nil, err } - key, err := UnwrappKey(priv, uid, c, len(c3)) + key, err := UnwrapKey(priv, uid, c, len(c3)) if err != nil { return nil, err } @@ -543,7 +543,7 @@ func DecryptASN1(priv *EncryptPrivateKey, uid, ciphertext []byte) ([]byte, error 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 { return nil, err } diff --git a/sm9/sm9_test.go b/sm9/sm9_test.go index 3d3a5d4..774cfae 100644 --- a/sm9/sm9_test.go +++ b/sm9/sm9_test.go @@ -78,7 +78,7 @@ func TestSignASN1(t *testing.T) { } } -func TestWrappKey(t *testing.T) { +func TestWrapKey(t *testing.T) { masterKey, err := GenerateEncryptMasterKey(rand.Reader) hid := byte(0x01) uid := []byte("emmansun") @@ -89,12 +89,12 @@ func TestWrappKey(t *testing.T) { if err != nil { 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 { t.Fatal(err) } - key2, err := UnwrappKey(userKey, uid, cipher, 16) + key2, err := UnwrapKey(userKey, uid, cipher, 16) if err != nil { 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) hid := byte(0x01) uid := []byte("emmansun") @@ -115,12 +115,12 @@ func TestWrappKeyASN1(t *testing.T) { if err != nil { 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 { t.Fatal(err) } - key2, err := userKey.UnwrappKey(uid, cipher, 16) + key2, err := userKey.UnwrapKey(uid, cipher, 16) if err != nil { t.Fatal(err) } @@ -141,7 +141,7 @@ func TestUnmarshalSM9KeyPackage(t *testing.T) { if err != nil { 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 { t.Fatal(err) } @@ -151,7 +151,7 @@ func TestUnmarshalSM9KeyPackage(t *testing.T) { t.Fatal(err) } - key2, err := UnwrappKey(userKey, uid, cipher, 16) + key2, err := UnwrapKey(userKey, uid, cipher, 16) if err != nil { t.Fatal(err) } @@ -161,7 +161,7 @@ func TestUnmarshalSM9KeyPackage(t *testing.T) { } } -func TestWrappKeySM9Sample(t *testing.T) { +func TestWrapKeySM9Sample(t *testing.T) { expectedKey := "4ff5cf86d2ad40c8f4bac98d76abdbde0c0e2f0a829d3f911ef5b2bce0695480" masterKey := new(EncryptMasterPrivateKey) masterKey.D = bigFromHex("01EDEE3778F441F8DEA3D9FA0ACC4E07EE36C93F9A08618AF4AD85CEDE1C22") @@ -200,7 +200,7 @@ func TestWrappKeySM9Sample(t *testing.T) { 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 { t.Fatal(err) }