init: v1.0.0

This commit is contained in:
yaole
2026-05-27 23:03:00 +08:00
commit 8d97f750eb
466 changed files with 80067 additions and 0 deletions
+20
View File
@@ -0,0 +1,20 @@
// package gcltests provides algorithm rightness tests
// for SM{2,3,4,9}.
package gcltests
// func init() {
// if !SM2SignTest() ||
// !SM2VerifyTest() ||
// !SM3WithIDTest() ||
// !SM3Test() ||
// !SM4MacTest() ||
// !SM4EncECB() ||
// !SM4DecECB() ||
// !SM4EncCBC() ||
// !SM4DecCBC() ||
// !SM2TPCKeyGenTest() ||
// !SM2TPCSignTest() ||
// !SM2TPCDecTest() {
// panic("gcl tests failed")
// }
// }
+28
View File
@@ -0,0 +1,28 @@
package gcltests
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestRightness(t *testing.T) {
assert.True(t, SM2SignTest())
assert.True(t, SM2VerifyTest())
assert.True(t, SM2EncryptionTest())
assert.True(t, SM2DecryptionTest())
assert.True(t, SM3WithIDTest())
assert.True(t, SM3Test())
assert.True(t, SM4MacTest())
assert.True(t, SM4EncECB())
assert.True(t, SM4DecECB())
assert.True(t, SM4EncCBC())
assert.True(t, SM4DecCBC())
assert.True(t, SM2TPCKeyGenTest())
assert.True(t, SM2TPCSignTest())
assert.True(t, SM2TPCDecTest())
}
+104
View File
@@ -0,0 +1,104 @@
package gcltests
import (
"bytes"
"encoding/hex"
"xdx.jelly/xgcl/grand"
"xdx.jelly/xgcl/sm/sm2"
)
// 算法正确性测试
func hexDecode(s string) []byte {
b, err := hex.DecodeString(s)
if err != nil {
panic(err)
}
return b
}
// SM2SignTest SM2签名算法正确性测试
func SM2SignTest() bool {
// 先检测SM2签名验证
if !SM2VerifyTest() {
return false
}
sk := &sm2.PrivateKey{}
sk.SetBytes(hexDecode("C242939DDAB6FCC07B6676C07D2DC117EC68A09142C25C008630B9756786162D"))
pk := &sm2.PublicKey{}
pk.SetBytes(hexDecode("5CA4E440C508C45FE7D758AB10C45D8237C4F9559F7D466185F295399F0AA37D59AD8A3CD17903287681BF9D21DA2EB316A0CE8FD41C89CE1E2B3F1B8E041ABA"))
e := hexDecode("3854C463FA3F73783621B1CE4EF83F7C78048AAC79B221FCDD290866CC131174")
sig, err := sm2.Sign(e, grand.GetRandom(32), sk)
if err != nil {
return false
}
return sm2.Verify(e, pk, sig)
}
// SM2VerifyTest SM2签名算法正确性测试
func SM2VerifyTest() bool {
sk := &sm2.PrivateKey{}
sk.SetBytes(hexDecode("C242939DDAB6FCC07B6676C07D2DC117EC68A09142C25C008630B9756786162D"))
pk := &sm2.PublicKey{}
pk.SetBytes(hexDecode("5CA4E440C508C45FE7D758AB10C45D8237C4F9559F7D466185F295399F0AA37D59AD8A3CD17903287681BF9D21DA2EB316A0CE8FD41C89CE1E2B3F1B8E041ABA"))
e := hexDecode("3854C463FA3F73783621B1CE4EF83F7C78048AAC79B221FCDD290866CC131174")
sig := &sm2.Signature{}
sig.SetBytes(hexDecode("6E5DB49DBD0992B97040080A96003C721CDB9CF64C88D74321FC2F630ADF37742F6DFF453DFC8D7A506D3F52301BEE529E62FDDD38948F0D5D2CBCBC55900CFA"))
return sm2.Verify(e, pk, sig)
}
func SM2DecryptionTest() bool {
sk := &sm2.PrivateKey{}
sk.SetBytes(hexDecode("E7CB09606A53320B347F61F3F142DCB118F723A9BC27879F2805BE778F24AEE5"))
pk := &sm2.PublicKey{}
pk.SetBytes(hexDecode("C4F7D581BEFEF25C8BBB6DAD52A6AB8234FA7DB7A988592BC592DAF2BE630647E3746788CBDC59042D85260DD48B6A7347D82C5314E8AC261588A33151DFCA17"))
msg := hexDecode("31323334353637383132333435363738")
cipher := &sm2.Cipher{}
cipher.SetBytes(hexDecode("0E09E2BC01614B47F906C2A5F7F660F09DD2207B70E4AEF1B600A65BF273D0B1932E76AD4CE56EC20ED1D8F373E7E5AB09E2248104C7E7023D688F716730180DEF894C493E8D338D091F0F98F23A87AFE4E8AAA4F4FB4BE135B9A1B2073157B66B68E3ADFD914B61E61BA1FC65E5038D"))
plain, err := sm2.Decrypt(sk, cipher)
if err != nil {
return false
}
if !bytes.Equal(msg, plain) {
return false
}
return true
}
func SM2EncryptionTest() bool {
// 先进行SM2解密测试
if !SM2DecryptionTest() {
return false
}
sk := &sm2.PrivateKey{}
sk.SetBytes(hexDecode("E7CB09606A53320B347F61F3F142DCB118F723A9BC27879F2805BE778F24AEE5"))
pk := &sm2.PublicKey{}
pk.SetBytes(hexDecode("C4F7D581BEFEF25C8BBB6DAD52A6AB8234FA7DB7A988592BC592DAF2BE630647E3746788CBDC59042D85260DD48B6A7347D82C5314E8AC261588A33151DFCA17"))
msg := hexDecode("31323334353637383132333435363738")
cipher, err := sm2.Encrypt(pk, msg, grand.GetRandom(32))
if err != nil {
return false
}
plain, err := sm2.Decrypt(sk, cipher)
if err != nil {
return false
}
if !bytes.Equal(msg, plain) {
return false
}
return true
}
+119
View File
@@ -0,0 +1,119 @@
package gcltests
import (
"bytes"
"xdx.jelly/xgcl/grand"
"xdx.jelly/xgcl/sm/sm2"
"xdx.jelly/xgcl/tpc/sm2/sm2a"
)
// SM2协同签名密钥生成
func SM2TPCKeyGenTest() bool {
// 先进行SM2签名验证测试
if !SM2VerifyTest() {
return false
}
// 生成签名密钥分量和公钥
serverKeyCtx := sm2a.NewServerSignKeyGenContext()
clientKeyCtx := sm2a.NewClientSignKeyGenContext(grand.Reader)
buf, err := serverKeyCtx.ServerGenKey_one(grand.Reader)
if err != nil {
return false
}
buf, err = clientKeyCtx.ClientKeyGen_one(buf)
if err != nil {
return false
}
buf, err = serverKeyCtx.ServerGenKey_two(buf, grand.Reader)
if err != nil {
return false
}
buf, err = clientKeyCtx.ClientKeyGen_two(buf)
if err != nil {
return false
}
err = serverKeyCtx.ServerGenKey_three(buf)
if err != nil {
return false
}
dc, _ := clientKeyCtx.GetClientKey() // 客户端私钥分量
ds, _ := serverKeyCtx.GetServerKey() // 服务端私钥分量
pkc, _ := clientKeyCtx.GetPublicKey() // 客户端公钥
pks, _ := serverKeyCtx.GetPublicKey() // 服务端公钥
if !pkc.Equals(pks) {
return false
}
pk := pkc
// 协同计算签名
e := hexDecode("6F18CAF30D3E0C2F1C59DE6080BA23AF2F4DD49DE5173C4579B8A7FE03A57096")
clientSign := sm2a.NewClientSignContext(pk, grand.Reader)
buf, _ = clientSign.Initial(e)
buf, _ = sm2a.ServerSign(ds, pk, buf, grand.Reader)
sig, _ := clientSign.Final(dc, buf)
// 签名验证
if !sm2.Verify(e, pk, sig) {
return false
}
return true
}
// SM2协同签名验证
func SM2TPCSignTest() bool {
// 先进行SM2签名验证测试
if !SM2VerifyTest() {
return false
}
dc := &sm2.PrivateKey{}
dc.SetBytes(hexDecode("5F16B93817200830863BB55A523E131563C639880DA8D5F663C9CA32E872C621"))
ds := &sm2.PrivateKey{}
ds.SetBytes(hexDecode("5749BD354348F66F9905254E784C97BDE700DB7968219829F2DB5EC80D0AB0DF"))
pk := &sm2.PublicKey{}
pk.SetBytes(hexDecode("C31ED61795626AA8D8D26BB17359160F3CCB63786D50DF2C350C9DD27539DBDA5C2D7FAE4D9360CC77F9C0F7E66DB80ED35CF9969E68A0496BE1120020A4396A"))
// 协同计算签名
e := hexDecode("6F18CAF30D3E0C2F1C59DE6080BA23AF2F4DD49DE5173C4579B8A7FE03A57096")
clientSign := sm2a.NewClientSignContext(pk, grand.Reader)
buf, _ := clientSign.Initial(e)
buf, _ = sm2a.ServerSign(ds, pk, buf, grand.Reader)
sig, _ := clientSign.Final(dc, buf)
// 签名验证
if !sm2.Verify(e, pk, sig) {
return false
}
return true
}
// SM2协同解密验证
func SM2TPCDecTest() bool {
if !SM2EncryptionTest() {
return false
}
dc := &sm2.PrivateKey{}
dc.SetBytes(hexDecode("A17EE7749FC8882D876A1CCE1BCAB13A4F42E28E7EA30B6E81CD068806FE943C"))
ds := &sm2.PrivateKey{}
ds.SetBytes(hexDecode("566760699576E8B3882489B7FB9ED0DE4B467260665156EC131665D929684309"))
pk := &sm2.PublicKey{}
pk.SetBytes(hexDecode("2B2E90C71D9B16CC4F33D1775E76F5D7C0F283F2D7123504B718788FA38FDB2BBF7F7D94683C498947F365C91C42D7BDC10159E092C158B97BE7B035868949F8"))
stdPlain := hexDecode("31323334353637383132333435363738")
cipher := &sm2.Cipher{}
cipher.SetBytes(hexDecode("A098DB078468335D160DCE7A8876B88D56E16173421E96D455FED5039BB3A2F9E4E940F2BA4644C230E63087974EDA8B9C22B0FB116809395060FA73F970D5B3BB43CEDBABC75E8F4F925DC1B58DC9243B4A92668FBA980E4C73DA579B360479A1EEEAEE29BFBD171821EAECBECE675B"))
// 协同解密
clientDecCtx := sm2a.NewClientDecContext()
buf, _ := clientDecCtx.Decrypt_one(cipher)
buf, _ = sm2a.ServerDecrypt(buf, ds)
plain, _ := clientDecCtx.Decrypt_two(buf, dc)
if !bytes.Equal(plain, stdPlain) {
return false
}
return true
}
+26
View File
@@ -0,0 +1,26 @@
package gcltests
import (
"bytes"
"xdx.jelly/xgcl/sm/sm2"
"xdx.jelly/xgcl/sm/sm3"
)
func SM3Test() bool {
data := hexDecode("763AFC537F7876C2D0B59FDB68D762E90CEFD222BB358D0D6931867CE26538649BE3579A4004483EA5D84D005063F76FB1CE7E5F2F933B5ED757A718182F383C4D58291A6A5D8D07C081F66806031539093362D854883A8874F7B919925DABC74C173E2162F07E6780E311FF0AEF059AE620303DECB6289E97F72C018723C471")
stdDigest := hexDecode("d32b9d793b4966e18c351892e65277756ef0cc9d28db954c959fbb85e3948442")
digest := sm3.Sum(data)
return bytes.Equal(digest[:], stdDigest)
}
func SM3WithIDTest() bool {
pk := &sm2.PublicKey{}
pk.SetBytes(hexDecode("127E9EA1805767FD68BF73097231642B3044EEEBB0EBC9A3848100EF455BF5F945709988D547B9E7CB24F276709607A16E25E3B242E54C5FB54DB1F93EE3873E"))
id := hexDecode("D208DC5A50CC8144FA7F644D5708655F")
data := hexDecode("1670390FB93CC01CC2C880BB43454119")
stdDigest := hexDecode("FD8FD32ABB888167FD4502DA3F23AC39CA90C87907547D4E334A1CB96E98717B")
digest := sm2.PreComputeWithIdAndPubkeyAndMessage(id, data, pk)
return bytes.Equal(digest[:], stdDigest)
}
+70
View File
@@ -0,0 +1,70 @@
package gcltests
import (
"bytes"
"xdx.jelly/xgcl/sm/sm4"
)
func SM4MacTest() bool {
key := hexDecode("EEE8A0136E9DBC99F09E29EB5C79DAC1")
data := hexDecode("27B16EAF7D0341A1449A14A3D1BD69F9")
iv := hexDecode("6071A1EB4C784230A037909D33290854")
stdMac := hexDecode("F5CAF5A089C06942F83D1B9BB212F8AF")
mac, err := sm4.Mac(key, iv, data)
if err != nil {
return false
}
return bytes.Equal(mac, stdMac)
}
func SM4EncECB() bool {
key := hexDecode("C35242CC90CB75935A536F32149F5C35")
data := hexDecode("ECB57EE6D15BD06632CFE9FD09B822AF")
stdCipher := hexDecode("03D8C466A7C245971925E35540E9209F")
cipher, err := sm4.EncryptECB(nil, key, data)
if err != nil {
return false
}
return bytes.Equal(cipher, stdCipher)
}
func SM4DecECB() bool {
key := hexDecode("C35242CC90CB75935A536F32149F5C35")
stdData := hexDecode("ECB57EE6D15BD06632CFE9FD09B822AF")
cipher := hexDecode("03D8C466A7C245971925E35540E9209F")
data, err := sm4.DecryptECB(nil, key, cipher)
if err != nil {
return false
}
return bytes.Equal(data, stdData)
}
func SM4EncCBC() bool {
key := hexDecode("99CF573EFE8BE7F3277E800FF793F217")
iv := hexDecode("57C1AD66EFD690F45785A0176C42211B")
data := hexDecode("5B3ADAFFDC04A435D8582B5E04D22CB2920B85FBE0655E2C62DBF15EE6D9D2546D05CA85C78BF757AF9B10C3699879160E52A6060A9F910AA05CFA3F4A58B64D")
stdCipher := hexDecode("6A4320D8EC4A544116F5AD9C50EB33AC61A472F451AF2496E7C1761734907AC3A7D474CE21CFA8E64D5CB2D92659FCB4A7F8F5D5DAAFAEA0E3C6078176EB6DD3")
cipher, err := sm4.EncryptCBC(nil, iv, key, data)
if err != nil {
return false
}
return bytes.Equal(cipher, stdCipher)
}
func SM4DecCBC() bool {
key := hexDecode("99CF573EFE8BE7F3277E800FF793F217")
iv := hexDecode("57C1AD66EFD690F45785A0176C42211B")
stdData := hexDecode("5B3ADAFFDC04A435D8582B5E04D22CB2920B85FBE0655E2C62DBF15EE6D9D2546D05CA85C78BF757AF9B10C3699879160E52A6060A9F910AA05CFA3F4A58B64D")
cipher := hexDecode("6A4320D8EC4A544116F5AD9C50EB33AC61A472F451AF2496E7C1761734907AC3A7D474CE21CFA8E64D5CB2D92659FCB4A7F8F5D5DAAFAEA0E3C6078176EB6DD3")
data, err := sm4.DecryptCBC(nil, iv, key, cipher)
if err != nil {
return false
}
return bytes.Equal(data, stdData)
}