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

     1  package sm2test
     2  
     3  import (
     4  	"crypto/rand"
     5  	"fmt"
     6  	"gitee.com/ks-custle/core-gm/x509"
     7  	"io/ioutil"
     8  	"testing"
     9  
    10  	"gitee.com/ks-custle/core-gm/sm2"
    11  	"gitee.com/ks-custle/core-gm/sm2soft"
    12  )
    13  
    14  func TestSm2Sign(t *testing.T) {
    15  	privSoft, err := sm2soft.GenerateKey(rand.Reader)
    16  	if err != nil {
    17  		t.Fatal(err)
    18  	}
    19  	// 验证生成的公钥是否在自己的椭圆曲线上
    20  	fmt.Printf("Soft公钥是否在Soft的椭圆曲线上: %v\n", privSoft.Curve.IsOnCurve(privSoft.X, privSoft.Y))
    21  	privHard, err := sm2.GenerateKey(rand.Reader)
    22  	if err != nil {
    23  		t.Fatal(err)
    24  	}
    25  	// 验证生成的公钥是否在自己的椭圆曲线上
    26  	fmt.Printf("Hard公钥是否在Hard的椭圆曲线上: %v\n", privHard.Curve.IsOnCurve(privHard.X, privHard.Y))
    27  
    28  	// 验证生成的公钥是否在对方的椭圆曲线上
    29  	fmt.Printf("Hard公钥是否在Soft的椭圆曲线上: %v\n", privSoft.Curve.IsOnCurve(privHard.X, privHard.Y))
    30  	fmt.Printf("Soft公钥是否在Hard的椭圆曲线上: %v\n", privHard.Curve.IsOnCurve(privSoft.X, privSoft.Y))
    31  
    32  	// soft私钥转为hard私钥
    33  	privHardFromSoft := convertPrivFromSoft2Hard(privSoft)
    34  	// hard私钥转为soft私钥
    35  	privSoftFromHard := convertPrivFromHard2Soft(privHard)
    36  
    37  	// 定义明文
    38  	msg := []byte("12345,上山打老虎")
    39  	fmt.Printf("明文: %s\n", msg)
    40  
    41  	// fmt.Println("========== sm2soft使用soft公私钥签名及验签 ==========")
    42  	softSignBySoftPriv, err := privSoft.Sign(rand.Reader, msg, nil)
    43  	if err != nil {
    44  		t.Fatal(err)
    45  	}
    46  	ok1 := privSoft.PublicKey.Verify(msg, softSignBySoftPriv)
    47  	fmt.Printf("验签结果: %v\n", ok1)
    48  
    49  	// fmt.Println("========== sm2hard使用hard公私钥签名及验签 ==========")
    50  	hardSignByHardPriv, err := privHard.Sign(rand.Reader, msg, nil)
    51  	if err != nil {
    52  		t.Fatal(err)
    53  	}
    54  	ok2 := privHard.PublicKey.Verify(msg, hardSignByHardPriv)
    55  	fmt.Printf("验签结果: %v\n", ok2)
    56  
    57  	// fmt.Println("========== sm2soft使用hard公私钥签名及验签 ==========")
    58  	softSignByHardPriv, err := privSoftFromHard.Sign(rand.Reader, msg, nil)
    59  	if err != nil {
    60  		t.Fatal(err)
    61  	}
    62  	ok3 := privSoftFromHard.PublicKey.Verify(msg, softSignByHardPriv)
    63  	fmt.Printf("验签结果: %v\n", ok3)
    64  
    65  	// fmt.Println("========== sm2hard使用soft公私钥签名及验签 ==========")
    66  	hardSignBySoftPriv, err := privHardFromSoft.Sign(rand.Reader, msg, nil)
    67  	if err != nil {
    68  		t.Fatal(err)
    69  	}
    70  	ok4 := privHardFromSoft.PublicKey.Verify(msg, hardSignBySoftPriv)
    71  	fmt.Printf("验签结果: %v\n", ok4)
    72  
    73  	// fmt.Println("========== sm2soft使用soft私钥签名, sm2hard使用soft公钥验签 ==========")
    74  	softSignBySoftPriv2, err := privSoft.Sign(rand.Reader, msg, nil)
    75  	if err != nil {
    76  		t.Fatal(err)
    77  	}
    78  	ok5 := privHardFromSoft.PublicKey.Verify(msg, softSignBySoftPriv2)
    79  	fmt.Printf("验签结果: %v\n", ok5)
    80  
    81  	// fmt.Println("========== sm2soft使用hard私钥签名, sm2hard使用hard公钥验签 ==========")
    82  	softSignByHardPriv2, err := privSoftFromHard.Sign(rand.Reader, msg, nil)
    83  	if err != nil {
    84  		t.Fatal(err)
    85  	}
    86  	ok6 := privHard.PublicKey.Verify(msg, softSignByHardPriv2)
    87  	fmt.Printf("验签结果: %v\n", ok6)
    88  
    89  	// fmt.Println("========== sm2hard使用soft私钥签名, sm2soft使用soft公钥验签 ==========")
    90  	hardSignBySoftPriv2, err := privHardFromSoft.Sign(rand.Reader, msg, nil)
    91  	if err != nil {
    92  		t.Fatal(err)
    93  	}
    94  	ok7 := privSoft.PublicKey.Verify(msg, hardSignBySoftPriv2)
    95  	fmt.Printf("验签结果: %v\n", ok7)
    96  
    97  	// fmt.Println("========== sm2hard使用hard私钥签名, sm2soft使用hard公钥验签 ==========")
    98  	hardSignByHardPriv2, err := privHard.Sign(rand.Reader, msg, nil)
    99  	if err != nil {
   100  		t.Fatal(err)
   101  	}
   102  	ok8 := privSoftFromHard.PublicKey.Verify(msg, hardSignByHardPriv2)
   103  	fmt.Printf("验签结果: %v\n", ok8)
   104  
   105  }
   106  
   107  func TestSm2Encrypt(t *testing.T) {
   108  	privSoft, _ := sm2soft.GenerateKey(rand.Reader)
   109  	// 验证生成的公钥是否在自己的椭圆曲线上
   110  	fmt.Printf("Soft公钥是否在Soft的椭圆曲线上: %v\n", privSoft.Curve.IsOnCurve(privSoft.X, privSoft.Y))
   111  	privHard, _ := sm2.GenerateKey(rand.Reader)
   112  	// 验证生成的公钥是否在自己的椭圆曲线上
   113  	fmt.Printf("Hard公钥是否在Hard的椭圆曲线上: %v\n", privHard.Curve.IsOnCurve(privHard.X, privHard.Y))
   114  	// 验证生成的公钥是否在对方的椭圆曲线上
   115  	fmt.Printf("Hard公钥是否在Soft的椭圆曲线上: %v\n", privSoft.Curve.IsOnCurve(privHard.X, privHard.Y))
   116  	fmt.Printf("Soft公钥是否在Hard的椭圆曲线上: %v\n", privHard.Curve.IsOnCurve(privSoft.X, privSoft.Y))
   117  	// soft私钥转为hard私钥
   118  	privHardFromSoft := convertPrivFromSoft2Hard(privSoft)
   119  	// hard私钥转为soft私钥
   120  	privSoftFromHard := convertPrivFromHard2Soft(privHard)
   121  	// 定义明文
   122  	msg := []byte("12345,上山打老虎")
   123  	fmt.Printf("明文: %s\n", msg)
   124  
   125  	// fmt.Println("========== sm2Soft使用soft公钥加密, soft私钥解密 ==========")
   126  	softEncBySoft, err := privSoft.PublicKey.EncryptAsn1(msg, rand.Reader)
   127  	if err != nil {
   128  		t.Fatal(err)
   129  	}
   130  	softDecBySoft, err := privSoft.DecryptAsn1(softEncBySoft)
   131  	if err != nil {
   132  		t.Fatal(err)
   133  	}
   134  	fmt.Printf("解密结果: %s\n", softDecBySoft)
   135  
   136  	// fmt.Println("========== sm2Hard使用hard公钥加密, hard私钥解密 ==========")
   137  	hardEncByHard, err := privHard.PublicKey.EncryptAsn1(msg, rand.Reader)
   138  	if err != nil {
   139  		t.Fatal(err)
   140  	}
   141  	hardDecByHard, err := privHard.DecryptAsn1(hardEncByHard)
   142  	if err != nil {
   143  		t.Fatal(err)
   144  	}
   145  	fmt.Printf("解密结果: %s\n", hardDecByHard)
   146  
   147  	// fmt.Println("========== sm2Soft使用hard公钥加密, hard私钥解密 ==========")
   148  	softEncByHard, err := privSoftFromHard.PublicKey.EncryptAsn1(msg, rand.Reader)
   149  	if err != nil {
   150  		t.Fatal(err)
   151  	}
   152  	softDecByHard, err := privSoftFromHard.DecryptAsn1(softEncByHard)
   153  	if err != nil {
   154  		t.Fatal(err)
   155  	}
   156  	fmt.Printf("解密结果: %s\n", softDecByHard)
   157  
   158  	// fmt.Println("========== sm2Hard使用soft公钥加密, soft私钥解密 ==========")
   159  	hardEncBySoft, err := privHardFromSoft.PublicKey.EncryptAsn1(msg, rand.Reader)
   160  	if err != nil {
   161  		t.Fatal(err)
   162  	}
   163  	hardDecBySoft, err := privHardFromSoft.DecryptAsn1(hardEncBySoft)
   164  	if err != nil {
   165  		t.Fatal(err)
   166  	}
   167  	fmt.Printf("解密结果: %s\n", hardDecBySoft)
   168  
   169  	// fmt.Println("========== sm2Soft使用hard公钥加密, sm2Hard使用hard私钥解密 ==========")
   170  	softEncByHard1, err := privSoftFromHard.PublicKey.EncryptAsn1(msg, rand.Reader)
   171  	if err != nil {
   172  		t.Fatal(err)
   173  	}
   174  	hardDecByHard1, err := privHard.DecryptAsn1(softEncByHard1)
   175  	if err != nil {
   176  		t.Fatal(err)
   177  	}
   178  	fmt.Printf("解密结果: %s\n", hardDecByHard1)
   179  
   180  	// fmt.Println("========== sm2Soft使用soft公钥加密, sm2Hard使用soft私钥解密 ==========")
   181  	softEncBySoft1, err := privSoft.PublicKey.EncryptAsn1(msg, rand.Reader)
   182  	if err != nil {
   183  		t.Fatal(err)
   184  	}
   185  	hardDecBySoft1, err := privHardFromSoft.DecryptAsn1(softEncBySoft1)
   186  	if err != nil {
   187  		t.Fatal(err)
   188  	}
   189  	fmt.Printf("解密结果: %s\n", hardDecBySoft1)
   190  
   191  	// fmt.Println("========== sm2Hard使用hard公钥加密, sm2Soft使用hard私钥解密 ==========")
   192  	hardEncByHard1, err := privHard.PublicKey.EncryptAsn1(msg, rand.Reader)
   193  	if err != nil {
   194  		t.Fatal(err)
   195  	}
   196  	softDecByHard1, err := privSoftFromHard.DecryptAsn1(hardEncByHard1)
   197  	if err != nil {
   198  		t.Fatal(err)
   199  	}
   200  	fmt.Printf("解密结果: %s\n", softDecByHard1)
   201  
   202  	// fmt.Println("========== sm2Hard使用soft公钥加密, sm2Soft使用soft私钥解密 ==========")
   203  	hardEncBySoft1, err := privHardFromSoft.PublicKey.EncryptAsn1(msg, rand.Reader)
   204  	if err != nil {
   205  		t.Fatal(err)
   206  	}
   207  	softDecBySoft1, err := privSoft.DecryptAsn1(hardEncBySoft1)
   208  	if err != nil {
   209  		t.Fatal(err)
   210  	}
   211  	fmt.Printf("解密结果: %s\n", softDecBySoft1)
   212  }
   213  
   214  func convertPrivFromSoft2Hard(privSoft *sm2soft.PrivateKey) *sm2.PrivateKey {
   215  	privHard := &sm2.PrivateKey{}
   216  	privHard.D = privSoft.D
   217  	privHard.X = privSoft.X
   218  	privHard.Y = privSoft.Y
   219  	privHard.Curve = privSoft.Curve
   220  	return privHard
   221  }
   222  
   223  func convertPrivFromHard2Soft(privHard *sm2.PrivateKey) *sm2soft.PrivateKey {
   224  	privSoft := &sm2soft.PrivateKey{}
   225  	privSoft.D = privHard.D
   226  	privSoft.X = privHard.X
   227  	privSoft.Y = privHard.Y
   228  	privSoft.Curve = privHard.Curve
   229  	return privSoft
   230  }
   231  
   232  func Test_sm2keyWithOtherLanguage(t *testing.T) {
   233  	private, _ := sm2.GenerateKey(rand.Reader)
   234  	public := &private.PublicKey
   235  
   236  	derPriv, err := x509.MarshalPKCS8PrivateKey(private)
   237  	if err != nil {
   238  		t.Fatal(err)
   239  	}
   240  	err = ioutil.WriteFile("testdata/sm2_pri_key.der", derPriv, 0644)
   241  	if err != nil {
   242  		t.Fatal(err)
   243  	}
   244  
   245  	derPub, err := x509.MarshalPKIXPublicKey(public)
   246  	if err != nil {
   247  		t.Fatal(err)
   248  	}
   249  	err = ioutil.WriteFile("testdata/sm2_pub_key.der", derPub, 0644)
   250  	if err != nil {
   251  		t.Fatal(err)
   252  	}
   253  
   254  	// 生成私钥pem文件
   255  	_, err = x509.WritePrivateKeytoPemFile("testdata/sm2_pri_key.pem", private, nil)
   256  	if err != nil {
   257  		t.Fatal(err)
   258  	}
   259  	// 生成公钥pem文件
   260  	_, err = x509.WritePublicKeytoPemFile("testdata/sm2_pub_key.pem", public)
   261  	if err != nil {
   262  		t.Fatal(err)
   263  	}
   264  }