github.com/Hyperledger-TWGC/tjfoc-gm@v1.4.0/sm4/sm4_test.go (about)

     1  /*
     2  Copyright Suzhou Tongji Fintech Research Institute 2017 All Rights Reserved.
     3  Licensed under the Apache License, Version 2.0 (the "License");
     4  you may not use this file except in compliance with the License.
     5  You may obtain a copy of the License at
     6  
     7                   http://www.apache.org/licenses/LICENSE-2.0
     8  
     9  Unless required by applicable law or agreed to in writing, software
    10  distributed under the License is distributed on an "AS IS" BASIS,
    11  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  See the License for the specific language governing permissions and
    13  limitations under the License.
    14  */
    15  
    16  package sm4
    17  
    18  import (
    19  	"fmt"
    20  	"reflect"
    21  	"testing"
    22  )
    23  
    24  func TestSM4(t *testing.T) {
    25  	key := []byte("1234567890abcdef")
    26  	fmt.Printf("key = %v\n", key)
    27  	data := []byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10}
    28  	err := WriteKeyToPemFile("key.pem", key, nil)
    29  	if err != nil {
    30  		t.Fatalf("WriteKeyToPem error")
    31  	}
    32  	key, err = ReadKeyFromPemFile("key.pem", nil)
    33  	fmt.Printf("key = %v\n", key)
    34  	if err != nil {
    35  		t.Fatal(err)
    36  	}
    37  	fmt.Printf("data = %x\n", data)
    38  	ecbMsg, err := Sm4Ecb(key, data, true)
    39  	if err != nil {
    40  		t.Errorf("sm4 enc error:%s", err)
    41  		return
    42  	}
    43  	fmt.Printf("ecbMsg = %x\n", ecbMsg)
    44  	ecbDec, err := Sm4Ecb(key, ecbMsg, false)
    45  	if err != nil {
    46  		t.Errorf("sm4 dec error:%s", err)
    47  		return
    48  	}
    49  	fmt.Printf("ecbDec = %x\n", ecbDec)
    50  	if !testCompare(data, ecbDec) {
    51  		t.Errorf("sm4 self enc and dec failed")
    52  	}
    53  	cbcMsg, err := Sm4Cbc(key, data, true)
    54  	if err != nil {
    55  		t.Errorf("sm4 enc error:%s", err)
    56  	}
    57  	fmt.Printf("cbcMsg = %x\n", cbcMsg)
    58  	cbcDec, err := Sm4Cbc(key, cbcMsg, false)
    59  	if err != nil {
    60  		t.Errorf("sm4 dec error:%s", err)
    61  		return
    62  	}
    63  	fmt.Printf("cbcDec = %x\n", cbcDec)
    64  	if !testCompare(data, cbcDec) {
    65  		t.Errorf("sm4 self enc and dec failed")
    66  	}
    67  
    68  
    69  	cbcMsg, err = Sm4CFB(key, data, true)
    70  	if err != nil {
    71  		t.Errorf("sm4 enc error:%s", err)
    72  	}
    73  	fmt.Printf("cbcCFB = %x\n", cbcMsg)
    74  
    75  	cbcCfb, err := Sm4CFB(key, cbcMsg, false)
    76  	if err != nil {
    77  		t.Errorf("sm4 dec error:%s", err)
    78  		return
    79  	}
    80  	fmt.Printf("cbcCFB = %x\n", cbcCfb)
    81  
    82  	cbcMsg, err = Sm4OFB(key, data, true)
    83  	if err != nil {
    84  		t.Errorf("sm4 enc error:%s", err)
    85  	}
    86  	fmt.Printf("cbcOFB = %x\n", cbcMsg)
    87  
    88  	cbcOfc, err := Sm4OFB(key, cbcMsg, false)
    89  	if err != nil {
    90  		t.Errorf("sm4 dec error:%s", err)
    91  		return
    92  	}
    93  	fmt.Printf("cbcOFB = %x\n", cbcOfc)
    94  }
    95  
    96  func BenchmarkSM4(t *testing.B) {
    97  	t.ReportAllocs()
    98  	key := []byte("1234567890abcdef")
    99  	data := []byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10}
   100  	err := WriteKeyToPemFile("key.pem", key, nil)
   101  	if err != nil {
   102  		t.Fatalf("WriteKeyToPem error")
   103  	}
   104  	key, err = ReadKeyFromPemFile("key.pem", nil)
   105  	if err != nil {
   106  		t.Fatal(err)
   107  	}
   108  	c, err := NewCipher(key)
   109  	if err != nil {
   110  		t.Fatal(err)
   111  	}
   112  
   113  	for i := 0; i < t.N; i++ {
   114  		d0 := make([]byte, 16)
   115  		c.Encrypt(d0, data)
   116  		d1 := make([]byte, 16)
   117  		c.Decrypt(d1, d0)
   118  	}
   119  }
   120  
   121  func TestErrKeyLen(t *testing.T) {
   122  	fmt.Printf("\n--------------test key len------------------")
   123  	key := []byte("1234567890abcdefg")
   124  	_, err := NewCipher(key)
   125  	if err != nil {
   126  		fmt.Println("\nError key len !")
   127  	}
   128  	key = []byte("1234")
   129  	_, err = NewCipher(key)
   130  	if err != nil {
   131  		fmt.Println("Error key len !")
   132  	}
   133  	fmt.Println("------------------end----------------------")
   134  }
   135  
   136  func testCompare(key1, key2 []byte) bool {
   137  	if len(key1) != len(key2) {
   138  		return false
   139  	}
   140  	for i, v := range key1 {
   141  		if i == 1 {
   142  			fmt.Println("type of v", reflect.TypeOf(v))
   143  		}
   144  		a := key2[i]
   145  		if a != v {
   146  			return false
   147  		}
   148  	}
   149  	return true
   150  }
   151