1
15
16 package sm2
17
18 import (
19 "bytes"
20 "crypto/rand"
21 "fmt"
22 "io/ioutil"
23 "math/big"
24 "os"
25 "testing"
26 )
27
28 func TestSm2(t *testing.T) {
29 priv, err := GenerateKey(rand.Reader)
30 fmt.Println(priv)
31 if err != nil {
32 t.Fatal(err)
33 }
34 fmt.Printf("%v\n", priv.Curve.IsOnCurve(priv.X, priv.Y))
35 pub := &priv.PublicKey
36 msg := []byte("123456")
37 d0, err := pub.EncryptAsn1(msg, rand.Reader)
38 if err != nil {
39 fmt.Printf("Error: failed to encrypt %s: %v\n", msg, err)
40 return
41 }
42
43 d1, err := priv.DecryptAsn1(d0)
44 if err != nil {
45 fmt.Printf("Error: failed to decrypt: %v\n", err)
46 }
47 fmt.Printf("clear text = %s\n", d1)
48 d2, err :=Encrypt(pub,msg, rand.Reader,C1C2C3)
49 if err != nil {
50 fmt.Printf("Error: failed to encrypt %s: %v\n", msg, err)
51 return
52 }
53
54 d3, err := Decrypt(priv,d2,C1C2C3)
55 if err != nil {
56 fmt.Printf("Error: failed to decrypt: %v\n", err)
57 }
58 fmt.Printf("clear text = %s\n", d3)
59 msg, _ = ioutil.ReadFile("ifile")
60 sign, err := priv.Sign(rand.Reader, msg, nil)
61 if err != nil {
62 t.Fatal(err)
63 }
64
65 err = ioutil.WriteFile("TestResult", sign, os.FileMode(0644))
66 if err != nil {
67 t.Fatal(err)
68 }
69 signdata, _ := ioutil.ReadFile("TestResult")
70 ok := priv.Verify(msg, signdata)
71 if ok != true {
72 fmt.Printf("Verify error\n")
73 } else {
74 fmt.Printf("Verify ok\n")
75 }
76 pubKey := priv.PublicKey
77 ok = pubKey.Verify(msg, signdata)
78 if ok != true {
79 fmt.Printf("Verify error\n")
80 } else {
81 fmt.Printf("Verify ok\n")
82 }
83
84 }
85
86 func BenchmarkSM2(t *testing.B) {
87 t.ReportAllocs()
88 msg := []byte("test")
89 priv, err := GenerateKey(nil)
90 if err != nil {
91 t.Fatal(err)
92 }
93 t.ResetTimer()
94 for i := 0; i < t.N; i++ {
95 sign, err := priv.Sign(nil, msg, nil)
96 if err != nil {
97 t.Fatal(err)
98 }
99 priv.Verify(msg, sign)
100 }
101 }
102
103 func TestKEB2(t *testing.T) {
104 ida := []byte{'1', '2', '3', '4', '5', '6', '7', '8',
105 '1', '2', '3', '4', '5', '6', '7', '8'}
106 idb := []byte{'1', '2', '3', '4', '5', '6', '7', '8',
107 '1', '2', '3', '4', '5', '6', '7', '8'}
108 daBuf := []byte{0x81, 0xEB, 0x26, 0xE9, 0x41, 0xBB, 0x5A, 0xF1,
109 0x6D, 0xF1, 0x16, 0x49, 0x5F, 0x90, 0x69, 0x52,
110 0x72, 0xAE, 0x2C, 0xD6, 0x3D, 0x6C, 0x4A, 0xE1,
111 0x67, 0x84, 0x18, 0xBE, 0x48, 0x23, 0x00, 0x29}
112 dbBuf := []byte{0x78, 0x51, 0x29, 0x91, 0x7D, 0x45, 0xA9, 0xEA,
113 0x54, 0x37, 0xA5, 0x93, 0x56, 0xB8, 0x23, 0x38,
114 0xEA, 0xAD, 0xDA, 0x6C, 0xEB, 0x19, 0x90, 0x88,
115 0xF1, 0x4A, 0xE1, 0x0D, 0xEF, 0xA2, 0x29, 0xB5}
116 raBuf := []byte{0xD4, 0xDE, 0x15, 0x47, 0x4D, 0xB7, 0x4D, 0x06,
117 0x49, 0x1C, 0x44, 0x0D, 0x30, 0x5E, 0x01, 0x24,
118 0x00, 0x99, 0x0F, 0x3E, 0x39, 0x0C, 0x7E, 0x87,
119 0x15, 0x3C, 0x12, 0xDB, 0x2E, 0xA6, 0x0B, 0xB3}
120
121 rbBuf := []byte{0x7E, 0x07, 0x12, 0x48, 0x14, 0xB3, 0x09, 0x48,
122 0x91, 0x25, 0xEA, 0xED, 0x10, 0x11, 0x13, 0x16,
123 0x4E, 0xBF, 0x0F, 0x34, 0x58, 0xC5, 0xBD, 0x88,
124 0x33, 0x5C, 0x1F, 0x9D, 0x59, 0x62, 0x43, 0xD6}
125
126 expk := []byte{0x6C, 0x89, 0x34, 0x73, 0x54, 0xDE, 0x24, 0x84,
127 0xC6, 0x0B, 0x4A, 0xB1, 0xFD, 0xE4, 0xC6, 0xE5}
128
129 curve := P256Sm2()
130 curve.ScalarBaseMult(daBuf)
131 da := new(PrivateKey)
132 da.PublicKey.Curve = curve
133 da.D = new(big.Int).SetBytes(daBuf)
134 da.PublicKey.X, da.PublicKey.Y = curve.ScalarBaseMult(daBuf)
135
136 db := new(PrivateKey)
137 db.PublicKey.Curve = curve
138 db.D = new(big.Int).SetBytes(dbBuf)
139 db.PublicKey.X, db.PublicKey.Y = curve.ScalarBaseMult(dbBuf)
140
141 ra := new(PrivateKey)
142 ra.PublicKey.Curve = curve
143 ra.D = new(big.Int).SetBytes(raBuf)
144 ra.PublicKey.X, ra.PublicKey.Y = curve.ScalarBaseMult(raBuf)
145
146 rb := new(PrivateKey)
147 rb.PublicKey.Curve = curve
148 rb.D = new(big.Int).SetBytes(rbBuf)
149 rb.PublicKey.X, rb.PublicKey.Y = curve.ScalarBaseMult(rbBuf)
150
151 k1, Sb, S2, err := KeyExchangeB(16, ida, idb, db, &da.PublicKey, rb, &ra.PublicKey)
152 if err != nil {
153 t.Error(err)
154 }
155 k2, S1, Sa, err := KeyExchangeA(16, ida, idb, da, &db.PublicKey, ra, &rb.PublicKey)
156 if err != nil {
157 t.Error(err)
158 }
159 if bytes.Compare(k1, k2) != 0 {
160 t.Error("key exchange differ")
161 }
162 if bytes.Compare(k1, expk) != 0 {
163 t.Errorf("expected %x, found %x", expk, k1)
164 }
165 if bytes.Compare(S1, Sb) != 0 {
166 t.Error("hash verfication failed")
167 }
168 if bytes.Compare(Sa, S2) != 0 {
169 t.Error("hash verfication failed")
170 }
171 }
172
View as plain text