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