github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/sm2test/sm2_test.go (about) 1 package sm2test 2 3 import ( 4 "crypto/rand" 5 "encoding/hex" 6 "fmt" 7 "io/ioutil" 8 "math/big" 9 "testing" 10 11 "github.com/hxx258456/ccgo/sm2" 12 "github.com/hxx258456/ccgo/sm2soft" 13 "github.com/hxx258456/ccgo/x509" 14 ) 15 16 func TestSm2Sign(t *testing.T) { 17 privSoft, err := sm2soft.GenerateKey(rand.Reader) 18 if err != nil { 19 t.Fatal(err) 20 } 21 // 验证生成的公钥是否在自己的椭圆曲线上 22 fmt.Printf("Soft公钥是否在Soft的椭圆曲线上: %v\n", privSoft.Curve.IsOnCurve(privSoft.X, privSoft.Y)) 23 privHard, err := sm2.GenerateKey(rand.Reader) 24 if err != nil { 25 t.Fatal(err) 26 } 27 // 验证生成的公钥是否在自己的椭圆曲线上 28 fmt.Printf("Hard公钥是否在Hard的椭圆曲线上: %v\n", privHard.Curve.IsOnCurve(privHard.X, privHard.Y)) 29 30 // 验证生成的公钥是否在对方的椭圆曲线上 31 fmt.Printf("Hard公钥是否在Soft的椭圆曲线上: %v\n", privSoft.Curve.IsOnCurve(privHard.X, privHard.Y)) 32 fmt.Printf("Soft公钥是否在Hard的椭圆曲线上: %v\n", privHard.Curve.IsOnCurve(privSoft.X, privSoft.Y)) 33 34 // soft私钥转为hard私钥 35 privHardFromSoft := convertPrivFromSoft2Hard(privSoft) 36 // hard私钥转为soft私钥 37 privSoftFromHard := convertPrivFromHard2Soft(privHard) 38 39 // 定义明文 40 msg := []byte("12345,上山打老虎") 41 fmt.Printf("明文: %s\n", msg) 42 43 // fmt.Println("========== sm2soft使用soft公私钥签名及验签 ==========") 44 softSignBySoftPriv, err := privSoft.Sign(rand.Reader, msg, nil) 45 if err != nil { 46 t.Fatal(err) 47 } 48 ok1 := privSoft.PublicKey.Verify(msg, softSignBySoftPriv) 49 fmt.Printf("验签结果: %v\n", ok1) 50 51 // fmt.Println("========== sm2hard使用hard公私钥签名及验签 ==========") 52 hardSignByHardPriv, err := privHard.Sign(rand.Reader, msg, nil) 53 if err != nil { 54 t.Fatal(err) 55 } 56 ok2 := privHard.PublicKey.Verify(msg, hardSignByHardPriv) 57 fmt.Printf("验签结果: %v\n", ok2) 58 59 // fmt.Println("========== sm2soft使用hard公私钥签名及验签 ==========") 60 softSignByHardPriv, err := privSoftFromHard.Sign(rand.Reader, msg, nil) 61 if err != nil { 62 t.Fatal(err) 63 } 64 ok3 := privSoftFromHard.PublicKey.Verify(msg, softSignByHardPriv) 65 fmt.Printf("验签结果: %v\n", ok3) 66 67 // fmt.Println("========== sm2hard使用soft公私钥签名及验签 ==========") 68 hardSignBySoftPriv, err := privHardFromSoft.Sign(rand.Reader, msg, nil) 69 if err != nil { 70 t.Fatal(err) 71 } 72 ok4 := privHardFromSoft.PublicKey.Verify(msg, hardSignBySoftPriv) 73 fmt.Printf("验签结果: %v\n", ok4) 74 75 // fmt.Println("========== sm2soft使用soft私钥签名, sm2hard使用soft公钥验签 ==========") 76 softSignBySoftPriv2, err := privSoft.Sign(rand.Reader, msg, nil) 77 if err != nil { 78 t.Fatal(err) 79 } 80 ok5 := privHardFromSoft.PublicKey.Verify(msg, softSignBySoftPriv2) 81 fmt.Printf("验签结果: %v\n", ok5) 82 83 // fmt.Println("========== sm2soft使用hard私钥签名, sm2hard使用hard公钥验签 ==========") 84 softSignByHardPriv2, err := privSoftFromHard.Sign(rand.Reader, msg, nil) 85 if err != nil { 86 t.Fatal(err) 87 } 88 ok6 := privHard.PublicKey.Verify(msg, softSignByHardPriv2) 89 fmt.Printf("验签结果: %v\n", ok6) 90 91 // fmt.Println("========== sm2hard使用soft私钥签名, sm2soft使用soft公钥验签 ==========") 92 hardSignBySoftPriv2, err := privHardFromSoft.Sign(rand.Reader, msg, nil) 93 if err != nil { 94 t.Fatal(err) 95 } 96 ok7 := privSoft.PublicKey.Verify(msg, hardSignBySoftPriv2) 97 fmt.Printf("验签结果: %v\n", ok7) 98 99 // fmt.Println("========== sm2hard使用hard私钥签名, sm2soft使用hard公钥验签 ==========") 100 hardSignByHardPriv2, err := privHard.Sign(rand.Reader, msg, nil) 101 if err != nil { 102 t.Fatal(err) 103 } 104 ok8 := privSoftFromHard.PublicKey.Verify(msg, hardSignByHardPriv2) 105 fmt.Printf("验签结果: %v\n", ok8) 106 107 } 108 109 func TestSm2Encrypt(t *testing.T) { 110 privSoft, _ := sm2soft.GenerateKey(rand.Reader) 111 // 验证生成的公钥是否在自己的椭圆曲线上 112 fmt.Printf("Soft公钥是否在Soft的椭圆曲线上: %v\n", privSoft.Curve.IsOnCurve(privSoft.X, privSoft.Y)) 113 privHard, _ := sm2.GenerateKey(rand.Reader) 114 // 验证生成的公钥是否在自己的椭圆曲线上 115 fmt.Printf("Hard公钥是否在Hard的椭圆曲线上: %v\n", privHard.Curve.IsOnCurve(privHard.X, privHard.Y)) 116 // 验证生成的公钥是否在对方的椭圆曲线上 117 fmt.Printf("Hard公钥是否在Soft的椭圆曲线上: %v\n", privSoft.Curve.IsOnCurve(privHard.X, privHard.Y)) 118 fmt.Printf("Soft公钥是否在Hard的椭圆曲线上: %v\n", privHard.Curve.IsOnCurve(privSoft.X, privSoft.Y)) 119 // soft私钥转为hard私钥 120 privHardFromSoft := convertPrivFromSoft2Hard(privSoft) 121 // hard私钥转为soft私钥 122 privSoftFromHard := convertPrivFromHard2Soft(privHard) 123 // 定义明文 124 msg := []byte("12345,上山打老虎") 125 fmt.Printf("明文: %s\n", msg) 126 127 // fmt.Println("========== sm2Soft使用soft公钥加密, soft私钥解密 ==========") 128 softEncBySoft, err := privSoft.PublicKey.EncryptAsn1(msg, rand.Reader) 129 if err != nil { 130 t.Fatal(err) 131 } 132 softDecBySoft, err := privSoft.DecryptAsn1(softEncBySoft) 133 if err != nil { 134 t.Fatal(err) 135 } 136 fmt.Printf("解密结果: %s\n", softDecBySoft) 137 138 // fmt.Println("========== sm2Hard使用hard公钥加密, hard私钥解密 ==========") 139 hardEncByHard, err := privHard.PublicKey.EncryptAsn1(msg, rand.Reader) 140 if err != nil { 141 t.Fatal(err) 142 } 143 hardDecByHard, err := privHard.DecryptAsn1(hardEncByHard) 144 if err != nil { 145 t.Fatal(err) 146 } 147 fmt.Printf("解密结果: %s\n", hardDecByHard) 148 149 // fmt.Println("========== sm2Soft使用hard公钥加密, hard私钥解密 ==========") 150 softEncByHard, err := privSoftFromHard.PublicKey.EncryptAsn1(msg, rand.Reader) 151 if err != nil { 152 t.Fatal(err) 153 } 154 softDecByHard, err := privSoftFromHard.DecryptAsn1(softEncByHard) 155 if err != nil { 156 t.Fatal(err) 157 } 158 fmt.Printf("解密结果: %s\n", softDecByHard) 159 160 // fmt.Println("========== sm2Hard使用soft公钥加密, soft私钥解密 ==========") 161 hardEncBySoft, err := privHardFromSoft.PublicKey.EncryptAsn1(msg, rand.Reader) 162 if err != nil { 163 t.Fatal(err) 164 } 165 hardDecBySoft, err := privHardFromSoft.DecryptAsn1(hardEncBySoft) 166 if err != nil { 167 t.Fatal(err) 168 } 169 fmt.Printf("解密结果: %s\n", hardDecBySoft) 170 171 // fmt.Println("========== sm2Soft使用hard公钥加密, sm2Hard使用hard私钥解密 ==========") 172 softEncByHard1, err := privSoftFromHard.PublicKey.EncryptAsn1(msg, rand.Reader) 173 if err != nil { 174 t.Fatal(err) 175 } 176 hardDecByHard1, err := privHard.DecryptAsn1(softEncByHard1) 177 if err != nil { 178 t.Fatal(err) 179 } 180 fmt.Printf("解密结果: %s\n", hardDecByHard1) 181 182 // fmt.Println("========== sm2Soft使用soft公钥加密, sm2Hard使用soft私钥解密 ==========") 183 softEncBySoft1, err := privSoft.PublicKey.EncryptAsn1(msg, rand.Reader) 184 if err != nil { 185 t.Fatal(err) 186 } 187 hardDecBySoft1, err := privHardFromSoft.DecryptAsn1(softEncBySoft1) 188 if err != nil { 189 t.Fatal(err) 190 } 191 fmt.Printf("解密结果: %s\n", hardDecBySoft1) 192 193 // fmt.Println("========== sm2Hard使用hard公钥加密, sm2Soft使用hard私钥解密 ==========") 194 hardEncByHard1, err := privHard.PublicKey.EncryptAsn1(msg, rand.Reader) 195 if err != nil { 196 t.Fatal(err) 197 } 198 softDecByHard1, err := privSoftFromHard.DecryptAsn1(hardEncByHard1) 199 if err != nil { 200 t.Fatal(err) 201 } 202 fmt.Printf("解密结果: %s\n", softDecByHard1) 203 204 // fmt.Println("========== sm2Hard使用soft公钥加密, sm2Soft使用soft私钥解密 ==========") 205 hardEncBySoft1, err := privHardFromSoft.PublicKey.EncryptAsn1(msg, rand.Reader) 206 if err != nil { 207 t.Fatal(err) 208 } 209 softDecBySoft1, err := privSoft.DecryptAsn1(hardEncBySoft1) 210 if err != nil { 211 t.Fatal(err) 212 } 213 fmt.Printf("解密结果: %s\n", softDecBySoft1) 214 } 215 216 func convertPrivFromSoft2Hard(privSoft *sm2soft.PrivateKey) *sm2.PrivateKey { 217 privHard := &sm2.PrivateKey{} 218 privHard.D = privSoft.D 219 privHard.X = privSoft.X 220 privHard.Y = privSoft.Y 221 privHard.Curve = privSoft.Curve 222 return privHard 223 } 224 225 func convertPrivFromHard2Soft(privHard *sm2.PrivateKey) *sm2soft.PrivateKey { 226 privSoft := &sm2soft.PrivateKey{} 227 privSoft.D = privHard.D 228 privSoft.X = privHard.X 229 privSoft.Y = privHard.Y 230 privSoft.Curve = privHard.Curve 231 return privSoft 232 } 233 234 func Test_sm2keyWithOtherLanguage(t *testing.T) { 235 private, _ := sm2.GenerateKey(rand.Reader) 236 public := &private.PublicKey 237 238 derPriv, err := x509.MarshalPKCS8PrivateKey(private) 239 if err != nil { 240 t.Fatal(err) 241 } 242 err = ioutil.WriteFile("testdata/sm2_pri_key.der", derPriv, 0644) 243 if err != nil { 244 t.Fatal(err) 245 } 246 247 derPub, err := x509.MarshalPKIXPublicKey(public) 248 if err != nil { 249 t.Fatal(err) 250 } 251 err = ioutil.WriteFile("testdata/sm2_pub_key.der", derPub, 0644) 252 if err != nil { 253 t.Fatal(err) 254 } 255 256 // 生成私钥pem文件 257 _, err = x509.WritePrivateKeytoPemFile("testdata/sm2_pri_key.pem", private, nil) 258 if err != nil { 259 t.Fatal(err) 260 } 261 // 生成公钥pem文件 262 _, err = x509.WritePublicKeytoPemFile("testdata/sm2_pub_key.pem", public) 263 if err != nil { 264 t.Fatal(err) 265 } 266 } 267 268 // 根据已有加密信息生成密钥 269 func TestGenerateSm2(t *testing.T) { 270 msg := []byte("test") 271 272 // 函数曲线使用p256 273 c := sm2.P256Sm2() 274 275 publicX_hex := "0ce2fa6e66521155f780573beb0e5f18d0aeea6b9a145f54e5c8c442efd15ecf" 276 publicX_byte, err := hex.DecodeString(publicX_hex) 277 if err != nil { 278 t.Error(err) 279 } 280 publicY_hex := "fa332850bffd6e06cbbd6e29ac851fe12da302c74550c3d75e24db54a2a1fdd7" 281 publicY_byte, err := hex.DecodeString(publicY_hex) 282 if err != nil { 283 t.Error(err) 284 } 285 x := big.Int{} 286 y := big.Int{} 287 // 公钥 288 public := sm2.PublicKey{ 289 X: x.SetBytes(publicX_byte), 290 Y: y.SetBytes(publicY_byte), 291 Curve: c, 292 } 293 294 encByte, err := sm2.EncryptDefault(&public, msg, rand.Reader) 295 if err != nil { 296 t.Error(err) 297 } 298 fmt.Printf("加密结果: %x", encByte) 299 // encHex := "04090288425ca9800dda045f8bee4423f668cbbf2ad09b3aad4d7bb2ae7a0b2d0142dbaac88042a2c0c2c19e0c30d308a7e3e200692cd40d587aa409bba153083f8a37739de614648e22035d013df316af6ed4ba89ca2d2a7fb7b2deed7da51c789a72a6a7" 300 // encByte, err := hex.DecodeString(encHex) 301 // if err != nil { 302 // t.Error(err) 303 // } 304 305 private_hex := "0b15a775077e438bce6ebcb7b30c3e61d9909ee861568723661d4728ee701068" 306 private_byte, err := hex.DecodeString(private_hex) 307 if err != nil { 308 t.Error(err) 309 } 310 d := big.Int{} 311 private := sm2.PrivateKey{ 312 PublicKey: public, 313 D: d.SetBytes(private_byte), 314 } 315 316 decMsg, err := sm2.DecryptDefault(&private, encByte) 317 if err != nil { 318 t.Error(err) 319 } 320 fmt.Printf("解密结果: %s\n", decMsg) 321 }