gitee.com/zhaochuninhefei/gmgo@v0.0.31-0.20240209061119-069254a02979/sm4/sm4_test.go (about)

     1  // Copyright (c) 2022 zhaochun
     2  // gmgo 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  package sm4
    10  
    11  import (
    12  	"crypto/aes"
    13  	"crypto/cipher"
    14  	"encoding/hex"
    15  	"fmt"
    16  	"gitee.com/zhaochuninhefei/gmgo/utils"
    17  	"runtime"
    18  	"testing"
    19  
    20  	"gitee.com/zhaochuninhefei/zcgolog/zclog"
    21  	"golang.org/x/sys/cpu"
    22  )
    23  
    24  func TestSm4(t *testing.T) {
    25  	//key := []byte("1234567890abcdef")
    26  	key, err := utils.GetRandomBytes(16)
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  	fmt.Printf("密钥转为hex: %s\n", hex.EncodeToString(key))
    31  	data := []byte("天行健君子以自强不息")
    32  
    33  	fmt.Println("---------------- testCBC ----------------")
    34  	err = testCBC(key, data)
    35  	if err != nil {
    36  		t.Fatal(err)
    37  	}
    38  
    39  	fmt.Println("---------------- testCFB ----------------")
    40  	err = testCFB(key, data)
    41  	if err != nil {
    42  		t.Fatal(err)
    43  	}
    44  
    45  	fmt.Println("---------------- testOFB ----------------")
    46  	err = testOFB(key, data)
    47  	if err != nil {
    48  		t.Fatal(err)
    49  	}
    50  
    51  	fmt.Println("---------------- testGCM ----------------")
    52  	err = testGCM(key, data)
    53  	if err != nil {
    54  		t.Fatal(err)
    55  	}
    56  }
    57  
    58  func TestAesGCM1(t *testing.T) {
    59  	key, _ := hex.DecodeString("c64b7140c02e9cbe38626ea772794f57")
    60  	iv, _ := hex.DecodeString("11b413b9f5757aa64a803152")
    61  	ciphertext, _ := hex.DecodeString("863286881f10c94e642c7694ac605aa3427a14a84f6a681c056b21770f1b9abe241a2ecee0b8c369ffe16ad42b50ced2abd0bc90a161979b0f793371ebd53e97")
    62  
    63  	block, err := aes.NewCipher(key)
    64  	if err != nil {
    65  		panic(err.Error())
    66  	}
    67  
    68  	aesgcm, err := cipher.NewGCM(block)
    69  	if err != nil {
    70  		panic(err.Error())
    71  	}
    72  
    73  	plaintext, err := aesgcm.Open(nil, iv, ciphertext, nil)
    74  	if err != nil {
    75  		panic(err.Error())
    76  	}
    77  	fmt.Printf("plaintext: %s\n", plaintext)
    78  }
    79  
    80  func TestGcmAsmWithNonce(t *testing.T) {
    81  	zclog.Level = zclog.LOG_LEVEL_DEBUG
    82  	zclog.Debug("supportSM4:", supportSM4)
    83  	zclog.Debug("supportsAES:", supportsAES)
    84  	zclog.Debug("supportsGFMUL:", supportsGFMUL)
    85  	zclog.Debug("useAVX2:", useAVX2)
    86  
    87  	key := []byte{251, 160, 47, 88, 53, 110, 220, 7, 229, 174, 145, 250, 40, 34, 188, 237}
    88  	nonce := []byte{182, 244, 44, 22, 113, 249, 246, 127, 114, 94, 115, 60}
    89  	dst := []byte{23, 3, 3, 2, 191}
    90  	data := []byte{11, 0, 2, 170, 0, 0, 2, 166, 0, 2, 161, 48, 130, 2, 157, 48, 130, 2, 67, 160, 3, 2, 1, 2, 2, 17, 0, 179, 19, 43, 244, 221, 102, 20, 101, 125, 96, 139, 186, 249, 198, 195, 128, 48, 10, 6, 8, 42, 129, 28, 207, 85, 1, 131, 117, 48, 74, 49, 15, 48, 13, 6, 3, 85, 4, 10, 19, 6, 99, 97, 116, 101, 115, 116, 49, 20, 48, 18, 6, 3, 85, 4, 3, 19, 11, 99, 97, 46, 116, 101, 115, 116, 46, 99, 111, 109, 49, 11, 48, 9, 6, 3, 85, 4, 6, 19, 2, 67, 78, 49, 20, 48, 18, 6, 3, 85, 4, 8, 19, 11, 65, 110, 104, 117, 105, 32, 72, 101, 102, 101, 105, 48, 30, 23, 13, 50, 50, 48, 52, 49, 50, 48, 56, 53, 50, 48, 51, 90, 23, 13, 51, 50, 48, 52, 48, 57, 48, 57, 53, 50, 48, 51, 90, 48, 83, 49, 20, 48, 18, 6, 3, 85, 4, 10, 12, 11, 115, 101, 114, 118, 101, 114, 95, 116, 101, 115, 116, 49, 24, 48, 22, 6, 3, 85, 4, 3, 19, 15, 115, 101, 114, 118, 101, 114, 46, 116, 101, 115, 116, 46, 99, 111, 109, 49, 11, 48, 9, 6, 3, 85, 4, 6, 19, 2, 67, 78, 49, 20, 48, 18, 6, 3, 85, 4, 8, 19, 11, 65, 110, 104, 117, 105, 32, 72, 101, 102, 101, 105, 48, 90, 48, 20, 6, 8, 42, 129, 28, 207, 85, 1, 130, 45, 6, 8, 42, 129, 28, 207, 85, 1, 130, 45, 3, 66, 0, 4, 208, 246, 86, 87, 22, 133, 125, 168, 54, 91, 20, 197, 65, 195, 72, 121, 155, 195, 153, 47, 205, 174, 4, 237, 184, 164, 199, 171, 193, 125, 196, 244, 152, 160, 152, 212, 105, 20, 101, 74, 231, 154, 254, 71, 47, 116, 38, 82, 17, 16, 177, 44, 237, 56, 187, 48, 26, 125, 243, 220, 27, 128, 205, 173, 163, 129, 255, 48, 129, 252, 48, 14, 6, 3, 85, 29, 15, 1, 1, 255, 4, 4, 3, 2, 6, 192, 48, 29, 6, 3, 85, 29, 37, 4, 22, 48, 20, 6, 8, 43, 6, 1, 5, 5, 7, 3, 1, 6, 8, 43, 6, 1, 5, 5, 7, 3, 2, 48, 41, 6, 3, 85, 29, 14, 4, 34, 4, 32, 211, 20, 37, 161, 114, 121, 43, 88, 162, 253, 161, 74, 105, 189, 203, 192, 67, 227, 69, 174, 129, 131, 172, 208, 91, 24, 210, 108, 207, 72, 20, 121, 48, 43, 6, 3, 85, 29, 35, 4, 36, 48, 34, 128, 32, 72, 47, 170, 202, 171, 110, 250, 70, 1, 121, 23, 136, 94, 115, 82, 88, 94, 97, 91, 98, 5, 106, 154, 74, 111, 55, 129, 6, 143, 58, 220, 191, 48, 115, 6, 3, 85, 29, 17, 4, 108, 48, 106, 130, 15, 115, 101, 114, 118, 101, 114, 46, 116, 101, 115, 116, 46, 99, 111, 109, 130, 16, 116, 101, 115, 116, 46, 101, 120, 97, 109, 112, 108, 101, 46, 99, 111, 109, 129, 17, 103, 111, 112, 104, 101, 114, 64, 103, 111, 108, 97, 110, 103, 46, 111, 114, 103, 135, 4, 127, 0, 0, 1, 135, 16, 32, 1, 72, 96, 0, 0, 32, 1, 0, 0, 0, 0, 0, 0, 0, 104, 134, 26, 104, 116, 116, 112, 115, 58, 47, 47, 102, 111, 111, 46, 99, 111, 109, 47, 119, 105, 98, 98, 108, 101, 35, 102, 111, 111, 48, 10, 6, 8, 42, 129, 28, 207, 85, 1, 131, 117, 3, 72, 0, 48, 69, 2, 32, 118, 163, 224, 17, 60, 183, 70, 62, 5, 158, 223, 251, 62, 186, 40, 120, 53, 145, 196, 225, 9, 235, 5, 251, 224, 133, 172, 205, 181, 237, 2, 51, 2, 33, 0, 215, 113, 160, 193, 183, 1, 187, 104, 101, 175, 88, 66, 195, 191, 53, 200, 235, 175, 0, 33, 224, 189, 75, 215, 130, 219, 162, 54, 11, 183, 170, 216, 0, 0, 22}
    91  	err := testGCMWithNonce(key, data, nonce, dst)
    92  	if err != nil {
    93  		t.Fatal(err)
    94  	}
    95  }
    96  
    97  func testGCMWithNonce(key, data, nonce, dst []byte) error {
    98  	encryptData, err := Sm4EncryptGcmWithNonce(data, key, nonce, dst)
    99  	if err != nil {
   100  		return err
   101  	}
   102  	fmt.Printf("GCM encryptData : %v\n", encryptData)
   103  
   104  	plainData, err := Sm4DecryptGcmWithNonce(encryptData, key, nonce, dst)
   105  	if err != nil {
   106  		return err
   107  	}
   108  	fmt.Printf("GCM plainData : %v\n", plainData)
   109  	return nil
   110  }
   111  
   112  func testCBC(key, data []byte) error {
   113  	iv, encryptData, err := Sm4EncryptCbc(data, key)
   114  	if err != nil {
   115  		return err
   116  	}
   117  	fmt.Printf("CBC iv 16进制 : %x\n", iv)
   118  	fmt.Printf("CBC encryptData 16进制 : %x\n", encryptData)
   119  	fmt.Printf("CBC encryptData 长度 : %d\n", len(encryptData))
   120  
   121  	plainData, err := Sm4DecryptCbc(encryptData, key, iv)
   122  	if err != nil {
   123  		return err
   124  	}
   125  	fmt.Printf("CBC plainData : %s\n", plainData)
   126  
   127  	encryptDataWithIV, err := Sm4EncryptCbcWithIV(data, key, iv)
   128  	if err != nil {
   129  		return err
   130  	}
   131  	fmt.Printf("CBC encryptDataWithIV 16进制 : %x\n", encryptDataWithIV)
   132  	return nil
   133  }
   134  
   135  func testCFB(key, data []byte) error {
   136  	iv, encryptData, err := Sm4EncryptCfb(data, key)
   137  	if err != nil {
   138  		return err
   139  	}
   140  	fmt.Printf("CFB iv 16进制 : %x\n", iv)
   141  	fmt.Printf("CFB encryptData 16进制 : %x\n", encryptData)
   142  
   143  	plainData, err := Sm4DecryptCfb(encryptData, key, iv)
   144  	if err != nil {
   145  		return err
   146  	}
   147  	fmt.Printf("CFB plainData : %s\n", plainData)
   148  	return nil
   149  }
   150  
   151  func testOFB(key, data []byte) error {
   152  	iv, encryptData, err := Sm4EncryptOfb(data, key)
   153  	if err != nil {
   154  		return err
   155  	}
   156  	fmt.Printf("OFB iv 16进制 : %x\n", iv)
   157  	fmt.Printf("OFB encryptData 16进制 : %x\n", encryptData)
   158  
   159  	plainData, err := Sm4DecryptOfb(encryptData, key, iv)
   160  	if err != nil {
   161  		return err
   162  	}
   163  	fmt.Printf("OFB plainData : %s\n", plainData)
   164  	return nil
   165  }
   166  
   167  func testGCM(key, data []byte) error {
   168  	nonce, encryptData, err := Sm4EncryptGcm(data, key)
   169  	if err != nil {
   170  		return err
   171  	}
   172  	fmt.Printf("GCM nonce 16进制 : %x\n", nonce)
   173  	fmt.Printf("GCM encryptData 16进制 : %x\n", encryptData)
   174  
   175  	plainData, err := Sm4DecryptGcm(encryptData, key, nonce)
   176  	if err != nil {
   177  		return err
   178  	}
   179  	fmt.Printf("GCM plainData : %s\n", plainData)
   180  	return nil
   181  }
   182  
   183  func BenchmarkSm4(t *testing.B) {
   184  	key := []byte("1234567890abcdef")
   185  	data := []byte("天行健君子以自强不息")
   186  	t.ReportAllocs()
   187  	for i := 0; i < t.N; i++ {
   188  		nonce, encryptData, _ := Sm4EncryptGcm(data, key)
   189  		_, err := Sm4DecryptGcm(encryptData, key, nonce)
   190  		if err != nil {
   191  			t.Fatal(err)
   192  		}
   193  	}
   194  }
   195  
   196  func TestCheckArch(t *testing.T) {
   197  	zclog.Level = zclog.LOG_LEVEL_DEBUG
   198  	zclog.Debug("supportSM4:", supportSM4)
   199  	zclog.Debug("supportsAES:", supportsAES)
   200  	zclog.Debug("supportsGFMUL:", supportsGFMUL)
   201  	zclog.Debug("useAVX2:", useAVX2)
   202  	zclog.Debug("arch:", runtime.GOARCH)
   203  	zclog.Debug("cpu.X86.HasAVX2:", cpu.X86.HasAVX2)
   204  	zclog.Debug("cpu.X86.HasBMI2:", cpu.X86.HasBMI2)
   205  }