package interop
import (
"testing"
"time"
"crypto/rand"
"fmt"
ccs "github.com/Hyperledger-TWGC/ccs-gm/sm2"
ccsutils "github.com/Hyperledger-TWGC/ccs-gm/utils"
// pku "github.com/Hyperledger-TWGC/pku-gm/gmssl"
tj "github.com/Hyperledger-TWGC/tjfoc-gm/sm2"
tjx509 "github.com/Hyperledger-TWGC/tjfoc-gm/x509"
)
const base_format = "2006-01-02 15:04:05"
func TestSM2(t *testing.T) {
// generate a random string as data
time := time.Now()
str_time := time.Format(base_format)
msg := []byte(str_time)
fmt.Println(string(msg))
// generate key from tj
sm2PrivKey, err := tj.GenerateKey(rand.Reader)
Fatal(err, t)
pemBytes, err := tjx509.WritePrivateKeyToPem(sm2PrivKey, []byte("123"))
fmt.Println(string(pemBytes))
Fatal(err, t)
sm2pub := &sm2PrivKey.PublicKey
// ccs load priv key pem
ccsPrivKey, err := ccsutils.PEMtoPrivateKey(pemBytes,[]byte("123"));
Fatal(err, t)
fmt.Println("ccs load tj priv key")
test, err := ccsutils.PrivateKeyToPEM(ccsPrivKey,[]byte("123"))
fmt.Println(string(test))
// encrypt by tj
d0, err := sm2pub.EncryptAsn1(msg, rand.Reader)
Fatal(err, t)
fmt.Println(string(d0))
// decrypt by ccs
plain, err := ccs.Decrypt(d0, ccsPrivKey)
fmt.Println(string(plain))
// decrypt by pku
// assert decrypt same with original
// sign by tj
sign, err := sm2PrivKey.Sign(rand.Reader, msg, nil) // 签名
Fatal(err, t)
fmt.Println(sign)
// verify by ccs
// verify by pku
}
=== RUN TestSM2
2021-02-16 22:27:01
-----BEGIN ENCRYPTED PRIVATE KEY-----
MIH8MFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAgjB6uSjZtSHgICCAAw
DAYIKoZIhvcNAgcFADAdBglghkgBZQMEASoEEDL4r6PzVRM0516cqCig8AMEgaAP
RUpkWMBK5Qg09+jAgp7vq5ZO/cbmk+ATfQoQtlCZjL2aOc3a2ULOrsjdTrl++ED+
ai0AS0NK8bpjrSb8R8J9FHu34FLql8TipJX1Ca12d9VqGXPIUBkO6seSidNmRmii
0wXUZ2IBPS8mOGx8nnsn1smuqjS0wJz3KrppAtO9aySZk2YTXC/GsLnEyQuD6r42
KVzFsfCyq0sGB+i/A78J
-----END ENCRYPTED PRIVATE KEY-----
detail error msg
asn1: structure error: tags don't match (2 vs {class:0 tag:16 length:87 isCompound:true}) {optional:false explicit:false application:false private:false defaultValue:<nil> tag:<nil> stringType:0 timeType:0 set:false omitEmpty:false} int @2
TestSM2: util_test.go:14: asn1: structure error: tags don't match (2 vs {class:0 tag:16 length:87 isCompound:true}) {optional:false explicit:false application:false private:false defaultValue:<nil> tag:<nil> stringType:0 timeType:0 set:false omitEmpty:false} int @2
--- FAIL: TestSM2 (0.00s)
FAIL
FAIL command-line-arguments 0.744s
FAIL
for un encrypted key, seems able to load, but there some items missing fails following actions.
=== RUN TestSM2
2021-02-16 22:24:25
-----BEGIN PRIVATE KEY-----
MIGTAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBHkwdwIBAQQgSrQIzIfZ8Tx1ouwR
6W16+aqiFR0TyMw+t+uJR/toFBugCgYIKoEcz1UBgi2hRANCAASbhqSSBAKIzno2
DZnjrqQCTzzxPadb67IhCLkH1aXPrbAIJuUMeDpsVI0UFdMD/qwSm85oQGlrrjMF
QBT4Nwx3
-----END PRIVATE KEY-----
ccs load tj priv key
-----BEGIN PRIVATE KEY-----
MIGTAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBHkwdwIBAQQgSrQIzIfZ8Tx1ouwR
6W16+aqiFR0TyMw+t+uJR/toFBugCgYIKoEcz1UBgi2hRANCAASbhqSSBAKIzno2
DZnjrqQCTzzxPadb67IhCLkH1aXPrbAIJuUMeDpsVI0UFdMD/qwSm85oQGlrrjMF
QBT4Nwx3
-----END PRIVATE KEY-----
0| ]$�1����1=v�an��y�o)�]{ۺ�H!՝<!!F��`dhW+��;�=
Zύ
Q����Gq|2
��N��d(h���������d�˗'�)j "[��
--- FAIL: TestSM2 (0.02s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x4114351]
goroutine 6 [running]:
testing.tRunner.func1.1(0x41a8be0, 0x432c9b0)
/usr/local/Cellar/go/1.14.1/libexec/src/testing/testing.go:941 +0x3d0
testing.tRunner.func1(0xc000130360)
/usr/local/Cellar/go/1.14.1/libexec/src/testing/testing.go:944 +0x3f9
panic(0x41a8be0, 0x432c9b0)
/usr/local/Cellar/go/1.14.1/libexec/src/runtime/panic.go:967 +0x166
math/big.(*Int).Cmp(0x0, 0xc00000ea00, 0x1)
/usr/local/Cellar/go/1.14.1/libexec/src/math/big/int.go:328 +0x41
github.com/Hyperledger-TWGC/ccs-gm/sm2.maybeReduceModP(0x0, 0xc000239c20)
/Users/yuanyi/go/pkg/mod/github.com/!hyperledger-!t!w!g!c/[email protected]/sm2/sm2p256_amd64.go:222 +0x3e
github.com/Hyperledger-TWGC/ccs-gm/sm2.p256Curve.ScalarMult(0xc000012740, 0x0, 0x0, 0xc000016760, 0x20, 0x20, 0x0, 0x4175359)
/Users/yuanyi/go/pkg/mod/github.com/!hyperledger-!t!w!g!c/[email protected]/sm2/sm2p256_amd64.go:323 +0xa0
github.com/Hyperledger-TWGC/ccs-gm/sm2.Decrypt(0xc000024480, 0x7e, 0x7e, 0xc000012780, 0x1, 0x7f, 0x0, 0x0, 0x7e)
/Users/yuanyi/go/pkg/mod/github.com/!hyperledger-!t!w!g!c/[email protected]/sm2/sm2enc.go:129 +0x174
command-line-arguments.TestSM2(0xc000130360)
/Users/yuanyi/go/src/github.com/SamYuan1990/fabric-gm-plugins/interop/sm2Interop_test.go:44 +0x5c0
testing.tRunner(0xc000130360, 0x41e50e0)
/usr/local/Cellar/go/1.14.1/libexec/src/testing/testing.go:992 +0xdc
created by testing.(*T).Run
/usr/local/Cellar/go/1.14.1/libexec/src/testing/testing.go:1043 +0x357
FAIL command-line-arguments 1.305s
FAIL