github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/sm4/cipher_test.go (about)

     1  package sm4
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  type CryptTest struct {
     9  	key []byte
    10  	in  []byte
    11  	out []byte
    12  }
    13  
    14  var encryptTests = []CryptTest{
    15  	{
    16  		// Appendix 1.
    17  		[]byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10},
    18  		[]byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10},
    19  		[]byte{0x68, 0x1e, 0xdf, 0x34, 0xd2, 0x06, 0x96, 0x5e, 0x86, 0xb3, 0xe9, 0x4f, 0x53, 0x6e, 0x42, 0x46},
    20  	},
    21  }
    22  
    23  func Test_sample1(t *testing.T) {
    24  	src := []byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10}
    25  	expected := []byte{0x68, 0x1e, 0xdf, 0x34, 0xd2, 0x06, 0x96, 0x5e, 0x86, 0xb3, 0xe9, 0x4f, 0x53, 0x6e, 0x42, 0x46}
    26  	c, err := NewCipher(src)
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  	dst := make([]byte, 16)
    31  	c.Encrypt(dst, src)
    32  	if !reflect.DeepEqual(dst, expected) {
    33  		t.Errorf("expected=%v, result=%v\n", expected, dst)
    34  	}
    35  	c.Decrypt(dst, expected)
    36  	if !reflect.DeepEqual(dst, src) {
    37  		t.Errorf("expected=%v, result=%v\n", src, dst)
    38  	}
    39  }
    40  
    41  func Test_sample2(t *testing.T) {
    42  	src := []byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10}
    43  	expected := []byte{0x59, 0x52, 0x98, 0xc7, 0xc6, 0xfd, 0x27, 0x1f, 0x04, 0x02, 0xf8, 0x04, 0xc3, 0x3d, 0x3f, 0x66}
    44  	c, err := NewCipher(src)
    45  	if err != nil {
    46  		t.Fatal(err)
    47  	}
    48  	dst := make([]byte, 16)
    49  	copy(dst, src)
    50  	n := 1000000
    51  	if testing.Short() {
    52  		n = 1000
    53  		expected = []byte{215, 53, 233, 28, 197, 104, 156, 243, 18, 188, 193, 239, 183, 64, 232, 19}
    54  	}
    55  	for i := 0; i < n; i++ {
    56  		c.Encrypt(dst, dst)
    57  	}
    58  	if !reflect.DeepEqual(dst, expected) {
    59  		t.Errorf("expected=%v, result=%v\n", expected, dst)
    60  	}
    61  }
    62  
    63  func BenchmarkEncrypt(b *testing.B) {
    64  	tt := encryptTests[0]
    65  	c, err := NewCipher(tt.key)
    66  	if err != nil {
    67  		b.Fatal("NewCipher:", err)
    68  	}
    69  	out := make([]byte, len(tt.in))
    70  	b.SetBytes(int64(len(out)))
    71  	b.ResetTimer()
    72  	for i := 0; i < b.N; i++ {
    73  		c.Encrypt(out, tt.in)
    74  	}
    75  }
    76  
    77  func BenchmarkDecrypt(b *testing.B) {
    78  	tt := encryptTests[0]
    79  	c, err := NewCipher(tt.key)
    80  	if err != nil {
    81  		b.Fatal("NewCipher:", err)
    82  	}
    83  	out := make([]byte, len(tt.out))
    84  	b.SetBytes(int64(len(out)))
    85  	b.ResetTimer()
    86  	for i := 0; i < b.N; i++ {
    87  		c.Decrypt(out, tt.out)
    88  	}
    89  }
    90  
    91  func BenchmarkExpand(b *testing.B) {
    92  	tt := encryptTests[0]
    93  	c := &sm4Cipher{make([]uint32, rounds), make([]uint32, rounds)}
    94  	b.ResetTimer()
    95  	for i := 0; i < b.N; i++ {
    96  		expandKey(tt.key, c.enc, c.dec)
    97  	}
    98  }