...

Source file src/github.com/tjfoc/gmsm/sm4/sm4_test.go

Documentation: github.com/tjfoc/gmsm/sm4

     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  
    27  	fmt.Printf("key = %v\n", key)
    28  	data := []byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10}
    29  	err := WriteKeyToPemFile("key.pem", key, nil)
    30  	if err != nil {
    31  		t.Fatalf("WriteKeyToPem error")
    32  	}
    33  	key, err = ReadKeyFromPemFile("key.pem", nil)
    34  	fmt.Printf("key = %v\n", key)
    35  	if err != nil {
    36  		t.Fatal(err)
    37  	}
    38  	fmt.Printf("data = %x\n", data)
    39  	ecbMsg, err := Sm4Ecb(key, data, true)
    40  	if err != nil {
    41  		t.Errorf("sm4 enc error:%s", err)
    42  		return
    43  	}
    44  	fmt.Printf("ecbMsg = %x\n", ecbMsg)
    45  	iv := []byte("0000000000000000")
    46  	err = SetIV(iv)
    47  	fmt.Printf("err = %v\n", err)
    48  	ecbDec, err := Sm4Ecb(key, ecbMsg, false)
    49  	if err != nil {
    50  		t.Errorf("sm4 dec error:%s", err)
    51  		return
    52  	}
    53  	fmt.Printf("ecbDec = %x\n", ecbDec)
    54  	if !testCompare(data, ecbDec) {
    55  		t.Errorf("sm4 self enc and dec failed")
    56  	}
    57  	cbcMsg, err := Sm4Cbc(key, data, true)
    58  	if err != nil {
    59  		t.Errorf("sm4 enc error:%s", err)
    60  	}
    61  	fmt.Printf("cbcMsg = %x\n", cbcMsg)
    62  	cbcDec, err := Sm4Cbc(key, cbcMsg, false)
    63  	if err != nil {
    64  		t.Errorf("sm4 dec error:%s", err)
    65  		return
    66  	}
    67  	fmt.Printf("cbcDec = %x\n", cbcDec)
    68  	if !testCompare(data, cbcDec) {
    69  		t.Errorf("sm4 self enc and dec failed")
    70  	}
    71  
    72  	cbcMsg, err = Sm4CFB(key, data, true)
    73  	if err != nil {
    74  		t.Errorf("sm4 enc error:%s", err)
    75  	}
    76  	fmt.Printf("cbcCFB = %x\n", cbcMsg)
    77  
    78  	cbcCfb, err := Sm4CFB(key, cbcMsg, false)
    79  	if err != nil {
    80  		t.Errorf("sm4 dec error:%s", err)
    81  		return
    82  	}
    83  	fmt.Printf("cbcCFB = %x\n", cbcCfb)
    84  
    85  	cbcMsg, err = Sm4OFB(key, data, true)
    86  	if err != nil {
    87  		t.Errorf("sm4 enc error:%s", err)
    88  	}
    89  	fmt.Printf("cbcOFB = %x\n", cbcMsg)
    90  
    91  	cbcOfc, err := Sm4OFB(key, cbcMsg, false)
    92  	if err != nil {
    93  		t.Errorf("sm4 dec error:%s", err)
    94  		return
    95  	}
    96  	fmt.Printf("cbcOFB = %x\n", cbcOfc)
    97  }
    98  
    99  func BenchmarkSM4(t *testing.B) {
   100  	t.ReportAllocs()
   101  	key := []byte("1234567890abcdef")
   102  	data := []byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10}
   103  	err := WriteKeyToPemFile("key.pem", key, nil)
   104  	if err != nil {
   105  		t.Fatalf("WriteKeyToPem error")
   106  	}
   107  	key, err = ReadKeyFromPemFile("key.pem", nil)
   108  	if err != nil {
   109  		t.Fatal(err)
   110  	}
   111  	c, err := NewCipher(key)
   112  	if err != nil {
   113  		t.Fatal(err)
   114  	}
   115  
   116  	for i := 0; i < t.N; i++ {
   117  		d0 := make([]byte, 16)
   118  		c.Encrypt(d0, data)
   119  		d1 := make([]byte, 16)
   120  		c.Decrypt(d1, d0)
   121  	}
   122  }
   123  
   124  func TestErrKeyLen(t *testing.T) {
   125  	fmt.Printf("\n--------------test key len------------------")
   126  	key := []byte("1234567890abcdefg")
   127  	_, err := NewCipher(key)
   128  	if err != nil {
   129  		fmt.Println("\nError key len !")
   130  	}
   131  	key = []byte("1234")
   132  	_, err = NewCipher(key)
   133  	if err != nil {
   134  		fmt.Println("Error key len !")
   135  	}
   136  	fmt.Println("------------------end----------------------")
   137  }
   138  
   139  func testCompare(key1, key2 []byte) bool {
   140  	if len(key1) != len(key2) {
   141  		return false
   142  	}
   143  	for i, v := range key1 {
   144  		if i == 1 {
   145  			fmt.Println("type of v", reflect.TypeOf(v))
   146  		}
   147  		a := key2[i]
   148  		if a != v {
   149  			return false
   150  		}
   151  	}
   152  	return true
   153  }
   154  

View as plain text