gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/sm4soft/sm4_gcm_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  sm4soft 是sm4的纯软实现,基于tjfoc国密算法库`tjfoc/gmsm`做了少量修改。
    11  对应版权声明: thrid_licenses/github.com/tjfoc/gmsm/版权声明
    12  */
    13  
    14  package sm4soft
    15  
    16  import (
    17  	"bytes"
    18  	"fmt"
    19  	"testing"
    20  )
    21  
    22  func TestSM4GCM(t *testing.T) {
    23  	// 定义key,16字节
    24  	key := []byte("1234567890abcdef")
    25  	fmt.Printf("key字节数组 : %v\n", key)
    26  	fmt.Printf("key字符串 : %s\n", key)
    27  	// 定义IV,16字节
    28  	IV := []byte("1234def567890abc")
    29  	// IV := make([]byte, BlockSize)
    30  	fmt.Printf("iv字节数组 : %v\n", IV)
    31  	fmt.Printf("iv16进制 : %x\n", IV)
    32  	fmt.Printf("iv字符串 : %s\n", IV)
    33  
    34  	// 定义数据,长度必须是16字节的倍数
    35  	// data := []byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10}
    36  	data := []byte("天行健君子以自强不息12")
    37  	fmt.Printf("data字节数组 : %v\n", data)
    38  	fmt.Printf("data十六进制 : %x\n", data)
    39  	fmt.Printf("data字符串 : %s\n", data)
    40  
    41  	testA := [][]byte{ // the length of the A can be random
    42  		{},
    43  		{0x01, 0x23, 0x45, 0x67, 0x89},
    44  		{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10},
    45  	}
    46  	for _, A := range testA {
    47  		fmt.Printf("====== 附加鉴别数据 A : %x\n", A)
    48  		// gcm模式加密
    49  		gcmMsg, T, err := Sm4GCM(key, IV, data, A, true)
    50  		if err != nil {
    51  			t.Errorf("sm4 enc error:%s", err)
    52  		}
    53  		fmt.Printf("gcmMsg 16进制 : %x\n", gcmMsg)
    54  		// gcm模式解密
    55  		gcmDec, T_, err := Sm4GCM(key, IV, gcmMsg, A, false)
    56  		if err != nil {
    57  			t.Errorf("sm4 dec error:%s", err)
    58  		}
    59  		fmt.Printf("gcmDec : %s\n", gcmDec)
    60  
    61  		if bytes.Equal(T, T_) {
    62  			fmt.Println("鉴别成功")
    63  		}
    64  
    65  		//Failed Test : if we input the different A , that will be a falied result.
    66  		A = []byte{0x01, 0x32, 0x45, 0x67, 0xba, 0xab, 0xcd}
    67  		gcmDec, T_, err = Sm4GCM(key, IV, gcmMsg, A, false)
    68  		if err != nil {
    69  			t.Errorf("使用不同的附加鉴别数据后,Sm4GCM 解密失败 : %s", err)
    70  		} else {
    71  			fmt.Printf("使用不同的附加鉴别数据后,gcmDec : %s\n", gcmDec)
    72  		}
    73  		if !bytes.Equal(T, T_) {
    74  			fmt.Println("鉴别失败")
    75  		}
    76  	}
    77  
    78  }
    79  
    80  func BenchmarkSm4(t *testing.B) {
    81  	key := []byte("1234567890abcdef")
    82  	IV := []byte("1234def567890abc")
    83  	A := []byte{0x01, 0x23, 0x45, 0x67, 0x89}
    84  	data := []byte("天行健君子以自强不息12")
    85  	t.ReportAllocs()
    86  	for i := 0; i < t.N; i++ {
    87  		gcmMsg, _, _ := Sm4GCM(key, IV, data, A, true)
    88  		_, _, err := Sm4GCM(key, IV, gcmMsg, A, false)
    89  		if err != nil {
    90  			t.Fatal(err)
    91  		}
    92  	}
    93  }