gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/sm2soft/sm2_test.go (about)

     1  // Copyright (c) 2022 zhaochun
     2  // core-gm is licensed under Mulan PSL v2.
     3  // You can use this software according to the terms and conditions of the Mulan PSL v2.
     4  // You may obtain a copy of Mulan PSL v2 at:
     5  //          http://license.coscl.org.cn/MulanPSL2
     6  // THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
     7  // See the Mulan PSL v2 for more details.
     8  
     9  /*
    10  sm2soft 是sm2的纯软实现,基于tjfoc国密算法库`tjfoc/gmsm`做了少量修改。
    11  对应版权声明: thrid_licenses/github.com/tjfoc/gmsm/版权声明
    12  */
    13  
    14  package sm2soft
    15  
    16  import (
    17  	"bytes"
    18  	"crypto/rand"
    19  	"fmt"
    20  	"io/ioutil"
    21  	"math/big"
    22  	"os"
    23  	"testing"
    24  
    25  	"gitee.com/ks-custle/core-gm/sm3"
    26  )
    27  
    28  func TestSm2(t *testing.T) {
    29  	// 生成sm2密钥对
    30  	priv, err := GenerateKey(rand.Reader)
    31  	fmt.Println("私钥: ", priv.D)
    32  	if err != nil {
    33  		t.Fatal(err)
    34  	}
    35  	// 验证生成的公钥是否在sm2的椭圆曲线上
    36  	fmt.Printf("公钥是否在sm2的椭圆曲线上: %v\n", priv.Curve.IsOnCurve(priv.X, priv.Y))
    37  	// 公钥
    38  	pub := &priv.PublicKey
    39  	fmt.Println("公钥: ", pub.X, pub.Y)
    40  
    41  	// 定义明文
    42  	msg := []byte("12345,上山打老虎")
    43  	fmt.Printf("明文: %s\n", msg)
    44  
    45  	// 公钥加密,C1C3C2模式,结果asn1编码
    46  	d0, err := pub.EncryptAsn1(msg, rand.Reader)
    47  	if err != nil {
    48  		fmt.Printf("Error: failed to encrypt %s: %v\n", msg, err)
    49  		return
    50  	}
    51  	fmt.Printf("公钥加密结果(C1C3C2) : %v\n", d0)
    52  
    53  	// 私钥解密,C1C3C2模式,先asn1解码
    54  	d1, err := priv.DecryptAsn1(d0)
    55  	if err != nil {
    56  		fmt.Printf("Error: failed to decrypt: %v\n", err)
    57  	}
    58  	fmt.Printf("私钥解密结果(C1C3C2) : %s\n", d1)
    59  
    60  	// 公钥加密 C1C2C3
    61  	d2, err := Encrypt(pub, msg, rand.Reader, C1C2C3)
    62  	if err != nil {
    63  		fmt.Printf("Error: failed to encrypt %s: %v\n", msg, err)
    64  		return
    65  	}
    66  	fmt.Printf("公钥加密结果(C1C2C3) : %v\n", d2)
    67  	// 私钥解密,C1C2C3
    68  	d3, err := Decrypt(priv, d2, C1C2C3)
    69  	if err != nil {
    70  		fmt.Printf("Error: failed to decrypt: %v\n", err)
    71  	}
    72  	fmt.Printf("私钥解密结果(C1C2C3) : %s\n", d3)
    73  
    74  	// 从文件读取消息
    75  	msg, _ = ioutil.ReadFile("testdata/msg")
    76  	hashFunc := sm3.New()
    77  	hashFunc.Write(msg)
    78  	digest := hashFunc.Sum(nil)
    79  
    80  	// 私钥签名
    81  	sign, err := priv.Sign(rand.Reader, digest, nil)
    82  	if err != nil {
    83  		t.Fatal(err)
    84  	}
    85  	// 签名写入文件
    86  	err = ioutil.WriteFile("testdata/signdata", sign, os.FileMode(0644))
    87  	if err != nil {
    88  		t.Fatal(err)
    89  	}
    90  	// 读取签名文件
    91  	signdata, _ := ioutil.ReadFile("testdata/signdata")
    92  	// 公钥验签
    93  	ok := pub.Verify(digest, signdata)
    94  	if ok != true {
    95  		fmt.Printf("公钥验签失败\n")
    96  	} else {
    97  		fmt.Printf("公钥验签成功\n")
    98  	}
    99  }
   100  
   101  func BenchmarkSM2(t *testing.B) {
   102  	t.ReportAllocs()
   103  	msg := []byte("test")
   104  	priv, err := GenerateKey(nil) // 生成密钥对
   105  	if err != nil {
   106  		t.Fatal(err)
   107  	}
   108  	t.ResetTimer()
   109  	for i := 0; i < t.N; i++ {
   110  		sign, err := priv.Sign(nil, msg, nil) // 签名
   111  		if err != nil {
   112  			t.Fatal(err)
   113  		}
   114  		priv.PublicKey.Verify(msg, sign) // 密钥验证
   115  	}
   116  }
   117  
   118  func BenchmarkVerify_SM2(b *testing.B) {
   119  	priv, err := GenerateKey(rand.Reader)
   120  	if err != nil {
   121  		b.Fatal(err)
   122  	}
   123  	hashed := []byte("testing")
   124  	r, s, err := Sm2Sign(priv, hashed, nil, rand.Reader)
   125  	if err != nil {
   126  		b.Fatal(err)
   127  	}
   128  
   129  	b.ReportAllocs()
   130  	b.ResetTimer()
   131  	for i := 0; i < b.N; i++ {
   132  		if !Sm2Verify(&priv.PublicKey, hashed, nil, r, s) {
   133  			b.Fatal("verify failed")
   134  		}
   135  	}
   136  }
   137  
   138  func TestKEB2(t *testing.T) {
   139  	ida := []byte{'1', '2', '3', '4', '5', '6', '7', '8',
   140  		'1', '2', '3', '4', '5', '6', '7', '8'}
   141  	idb := []byte{'1', '2', '3', '4', '5', '6', '7', '8',
   142  		'1', '2', '3', '4', '5', '6', '7', '8'}
   143  	daBuf := []byte{0x81, 0xEB, 0x26, 0xE9, 0x41, 0xBB, 0x5A, 0xF1,
   144  		0x6D, 0xF1, 0x16, 0x49, 0x5F, 0x90, 0x69, 0x52,
   145  		0x72, 0xAE, 0x2C, 0xD6, 0x3D, 0x6C, 0x4A, 0xE1,
   146  		0x67, 0x84, 0x18, 0xBE, 0x48, 0x23, 0x00, 0x29}
   147  	dbBuf := []byte{0x78, 0x51, 0x29, 0x91, 0x7D, 0x45, 0xA9, 0xEA,
   148  		0x54, 0x37, 0xA5, 0x93, 0x56, 0xB8, 0x23, 0x38,
   149  		0xEA, 0xAD, 0xDA, 0x6C, 0xEB, 0x19, 0x90, 0x88,
   150  		0xF1, 0x4A, 0xE1, 0x0D, 0xEF, 0xA2, 0x29, 0xB5}
   151  	raBuf := []byte{0xD4, 0xDE, 0x15, 0x47, 0x4D, 0xB7, 0x4D, 0x06,
   152  		0x49, 0x1C, 0x44, 0x0D, 0x30, 0x5E, 0x01, 0x24,
   153  		0x00, 0x99, 0x0F, 0x3E, 0x39, 0x0C, 0x7E, 0x87,
   154  		0x15, 0x3C, 0x12, 0xDB, 0x2E, 0xA6, 0x0B, 0xB3}
   155  
   156  	rbBuf := []byte{0x7E, 0x07, 0x12, 0x48, 0x14, 0xB3, 0x09, 0x48,
   157  		0x91, 0x25, 0xEA, 0xED, 0x10, 0x11, 0x13, 0x16,
   158  		0x4E, 0xBF, 0x0F, 0x34, 0x58, 0xC5, 0xBD, 0x88,
   159  		0x33, 0x5C, 0x1F, 0x9D, 0x59, 0x62, 0x43, 0xD6}
   160  
   161  	expk := []byte{0x6C, 0x89, 0x34, 0x73, 0x54, 0xDE, 0x24, 0x84,
   162  		0xC6, 0x0B, 0x4A, 0xB1, 0xFD, 0xE4, 0xC6, 0xE5}
   163  
   164  	curve := P256Sm2()
   165  	curve.ScalarBaseMult(daBuf)
   166  	da := new(PrivateKey)
   167  	da.PublicKey.Curve = curve
   168  	da.D = new(big.Int).SetBytes(daBuf)
   169  	da.PublicKey.X, da.PublicKey.Y = curve.ScalarBaseMult(daBuf)
   170  
   171  	db := new(PrivateKey)
   172  	db.PublicKey.Curve = curve
   173  	db.D = new(big.Int).SetBytes(dbBuf)
   174  	db.PublicKey.X, db.PublicKey.Y = curve.ScalarBaseMult(dbBuf)
   175  
   176  	ra := new(PrivateKey)
   177  	ra.PublicKey.Curve = curve
   178  	ra.D = new(big.Int).SetBytes(raBuf)
   179  	ra.PublicKey.X, ra.PublicKey.Y = curve.ScalarBaseMult(raBuf)
   180  
   181  	rb := new(PrivateKey)
   182  	rb.PublicKey.Curve = curve
   183  	rb.D = new(big.Int).SetBytes(rbBuf)
   184  	rb.PublicKey.X, rb.PublicKey.Y = curve.ScalarBaseMult(rbBuf)
   185  
   186  	k1, Sb, S2, err := KeyExchangeB(16, ida, idb, db, &da.PublicKey, rb, &ra.PublicKey)
   187  	if err != nil {
   188  		t.Error(err)
   189  	}
   190  	k2, S1, Sa, err := KeyExchangeA(16, ida, idb, da, &db.PublicKey, ra, &rb.PublicKey)
   191  	if err != nil {
   192  		t.Error(err)
   193  	}
   194  	if !bytes.Equal(k1, k2) {
   195  		t.Error("key exchange differ")
   196  	}
   197  	if !bytes.Equal(k1, expk) {
   198  		t.Errorf("expected %x, found %x", expk, k1)
   199  	}
   200  	if !bytes.Equal(S1, Sb) {
   201  		t.Error("hash verfication failed")
   202  	}
   203  	if !bytes.Equal(Sa, S2) {
   204  		t.Error("hash verfication failed")
   205  	}
   206  }