github.com/emmansun/gmsm@v0.29.1/zuc/eia256_test.go (about)

     1  package zuc
     2  
     3  import (
     4  	"encoding/hex"
     5  	"hash"
     6  	"testing"
     7  
     8  	"github.com/emmansun/gmsm/internal/cryptotest"
     9  )
    10  
    11  var zucEIA256Tests = []struct {
    12  	key    []byte
    13  	iv     []byte
    14  	msg    []byte
    15  	nMsgs  int
    16  	mac32  string
    17  	mac64  string
    18  	mac128 string
    19  }{
    20  	{
    21  		[]byte{
    22  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    23  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    24  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    25  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    26  		},
    27  		[]byte{
    28  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    29  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    30  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    31  		},
    32  		[]byte{
    33  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    34  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    35  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    36  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    37  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    38  		},
    39  		1,
    40  		"9b972a74",
    41  		"673e54990034d38c",
    42  		"d85e54bbcb9600967084c952a1654b26",
    43  	},
    44  	{
    45  		[]byte{
    46  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    47  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    48  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    49  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    50  		},
    51  		[]byte{
    52  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    53  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    54  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    55  		},
    56  		[]byte{
    57  			0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
    58  			0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
    59  			0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
    60  			0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
    61  			0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
    62  		},
    63  		10,
    64  		"8754f5cf",
    65  		"130dc225e72240cc",
    66  		"df1e8307b31cc62beca1ac6f8190c22f",
    67  	},
    68  	{
    69  		[]byte{
    70  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    71  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    72  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    73  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    74  		},
    75  		[]byte{
    76  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    77  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    78  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    79  		},
    80  		[]byte{
    81  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    82  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    83  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    84  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    85  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    86  		},
    87  		1,
    88  		"1f3079b4",
    89  		"8c71394d39957725",
    90  		"a35bb274b567c48b28319f111af34fbd",
    91  	},
    92  	{
    93  		[]byte{
    94  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    95  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    96  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    97  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    98  		},
    99  		[]byte{
   100  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
   101  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
   102  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
   103  		},
   104  		[]byte{
   105  			0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
   106  			0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
   107  			0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
   108  			0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
   109  			0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
   110  		},
   111  		10,
   112  		"5c7c8b88",
   113  		"ea1dee544bb6223b",
   114  		"3a83b554be408ca5494124ed9d473205",
   115  	},
   116  }
   117  
   118  func TestEIA_Finish256_32(t *testing.T) {
   119  	for i, test := range zucEIA256Tests {
   120  		h, err := NewHash256(test.key, test.iv, 4)
   121  		if err != nil {
   122  			t.Error(err)
   123  		}
   124  		for j := 0; j < test.nMsgs; j++ {
   125  			_, err = h.Write(test.msg)
   126  			if err != nil {
   127  				t.Error(err)
   128  			}
   129  		}
   130  		digest := h.Sum(nil)
   131  		if hex.EncodeToString(digest) != test.mac32 {
   132  			t.Errorf("case %d, expected=%s, result=%s\n", i+1, test.mac32, hex.EncodeToString(digest))
   133  		}
   134  		h.Reset()
   135  		for j := 0; j < test.nMsgs; j++ {
   136  			_, err = h.Write(test.msg)
   137  			if err != nil {
   138  				t.Error(err)
   139  			}
   140  		}
   141  		digest = h.Sum(nil)
   142  		if hex.EncodeToString(digest) != test.mac32 {
   143  			t.Errorf("case %d, expected=%s, result=%s\n", i+1, test.mac32, hex.EncodeToString(digest))
   144  		}
   145  	}
   146  }
   147  
   148  func TestEIA_Finish256_64(t *testing.T) {
   149  	for i, test := range zucEIA256Tests {
   150  		h, err := NewHash256(test.key, test.iv, 8)
   151  		if err != nil {
   152  			t.Error(err)
   153  		}
   154  		for j := 0; j < test.nMsgs; j++ {
   155  			_, err = h.Write(test.msg)
   156  			if err != nil {
   157  				t.Error(err)
   158  			}
   159  		}
   160  		digest := h.Sum(nil)
   161  		if hex.EncodeToString(digest) != test.mac64 {
   162  			t.Errorf("case %d, expected=%s, result=%s\n", i+1, test.mac64, hex.EncodeToString(digest))
   163  		}
   164  		h.Reset()
   165  		for j := 0; j < test.nMsgs; j++ {
   166  			_, err = h.Write(test.msg)
   167  			if err != nil {
   168  				t.Error(err)
   169  			}
   170  		}
   171  		digest = h.Sum(nil)
   172  		if hex.EncodeToString(digest) != test.mac64 {
   173  			t.Errorf("case %d, expected=%s, result=%s\n", i+1, test.mac64, hex.EncodeToString(digest))
   174  		}
   175  	}
   176  }
   177  
   178  func TestEIA_Finish256_128(t *testing.T) {
   179  	for i, test := range zucEIA256Tests {
   180  		h, err := NewHash256(test.key, test.iv, 16)
   181  		if err != nil {
   182  			t.Error(err)
   183  		}
   184  		for j := 0; j < test.nMsgs; j++ {
   185  			_, err = h.Write(test.msg)
   186  			if err != nil {
   187  				t.Error(err)
   188  			}
   189  		}
   190  		digest := h.Sum(nil)
   191  		if hex.EncodeToString(digest) != test.mac128 {
   192  			t.Errorf("case %d, expected=%s, result=%s\n", i+1, test.mac128, hex.EncodeToString(digest))
   193  		}
   194  		h.Reset()
   195  		for j := 0; j < test.nMsgs; j++ {
   196  			_, err = h.Write(test.msg)
   197  			if err != nil {
   198  				t.Error(err)
   199  			}
   200  		}
   201  		digest = h.Sum(nil)
   202  		if hex.EncodeToString(digest) != test.mac128 {
   203  			t.Errorf("case %d, expected=%s, result=%s\n", i+1, test.mac128, hex.EncodeToString(digest))
   204  		}
   205  	}
   206  }
   207  
   208  func TestEIA256_Sum32(t *testing.T) {
   209  	expected := "f4f20d7c"
   210  	h, err := NewHash256(zucEIA256Tests[2].key, zucEIA256Tests[2].iv, 4)
   211  	if err != nil {
   212  		t.Fatal(err)
   213  	}
   214  	_, err = h.Write([]byte("emmansun"))
   215  	if err != nil {
   216  		t.Fatal(err)
   217  	}
   218  	_, err = h.Write([]byte("shangmi1"))
   219  	if err != nil {
   220  		t.Fatal(err)
   221  	}
   222  	_, err = h.Write([]byte("emmansun shangmi"))
   223  	if err != nil {
   224  		t.Fatal(err)
   225  	}
   226  	_, err = h.Write([]byte("emmansun shangmi 1234"))
   227  	if err != nil {
   228  		t.Fatal(err)
   229  	}
   230  	mac := h.Sum(nil)
   231  	if hex.EncodeToString(mac) != expected {
   232  		t.Errorf("expected=%s, result=%s\n", expected, hex.EncodeToString(mac))
   233  	}
   234  }
   235  
   236  func TestEIA256_Finish(t *testing.T) {
   237  	expected := []struct {
   238  		expected string
   239  		macLen   int
   240  	}{
   241  		{
   242  			"9dd592c4",
   243  			4,
   244  		},
   245  		{
   246  			"1f6f71e386a2ce01",
   247  			8,
   248  		},
   249  		{
   250  			"bf5339cfd87bba97d70ef4f5973af8bb",
   251  			16,
   252  		},
   253  	}
   254  	for _, exp := range expected {
   255  		h, err := NewHash256(zucEIA256Tests[2].key, zucEIA256Tests[2].iv, exp.macLen)
   256  		if err != nil {
   257  			t.Fatal(err)
   258  		}
   259  		mac := h.Finish([]byte("emmansunshangmi1emmansun shangmiemmansun shangmi 12345"), 8*53+4)
   260  		if hex.EncodeToString(mac) != exp.expected {
   261  			t.Errorf("expected=%s, result=%s\n", exp.expected, hex.EncodeToString(mac))
   262  		}
   263  	}
   264  }
   265  
   266  func TestEIA256Hash(t *testing.T) {
   267  	t.Run("EIA-256-32", func(t *testing.T) {
   268  		cryptotest.TestHash(t, func() hash.Hash {
   269  			h, _ := NewHash256(zucEIA256Tests[0].key, zucEIA256Tests[0].iv, 4)
   270  			return h
   271  		})
   272  	})
   273  	t.Run("EIA-256-64", func(t *testing.T) {
   274  		cryptotest.TestHash(t, func() hash.Hash {
   275  			h, _ := NewHash256(zucEIA256Tests[0].key, zucEIA256Tests[0].iv, 8)
   276  			return h
   277  		})
   278  	})
   279  	t.Run("EIA-256-128", func(t *testing.T) {
   280  		cryptotest.TestHash(t, func() hash.Hash {
   281  			h, _ := NewHash256(zucEIA256Tests[0].key, zucEIA256Tests[0].iv, 16)
   282  			return h
   283  		})
   284  	})
   285  }
   286  
   287  func benchmark256Size(b *testing.B, size, tagSize int) {
   288  	var key [32]byte
   289  	var iv [23]byte
   290  	var buf = make([]byte, 8192)
   291  	bench, _ := NewHash256(key[:], iv[:], tagSize)
   292  	b.SetBytes(int64(size))
   293  	sum := make([]byte, bench.Size())
   294  	for i := 0; i < b.N; i++ {
   295  		bench.Reset()
   296  		bench.Write(buf[:size])
   297  		bench.Sum(sum[:0])
   298  	}
   299  }
   300  
   301  func BenchmarkHash8Bytes_Tag32(b *testing.B) {
   302  	benchmark256Size(b, 8, 4)
   303  }
   304  
   305  func BenchmarkHash8Bytes_Tag64(b *testing.B) {
   306  	benchmark256Size(b, 8, 8)
   307  }
   308  
   309  func BenchmarkHash8Bytes_Tag128(b *testing.B) {
   310  	benchmark256Size(b, 8, 16)
   311  }
   312  
   313  func BenchmarkHash1K_Tag32(b *testing.B) {
   314  	benchmark256Size(b, 1024, 4)
   315  }
   316  
   317  func BenchmarkHash1K_Tag64(b *testing.B) {
   318  	benchmark256Size(b, 1024, 8)
   319  }
   320  
   321  func BenchmarkHash1K_Tag128(b *testing.B) {
   322  	benchmark256Size(b, 1024, 16)
   323  }
   324  
   325  func BenchmarkHash8K_Tag32(b *testing.B) {
   326  	benchmark256Size(b, 8192, 4)
   327  }
   328  
   329  func BenchmarkHash8K_Tag64(b *testing.B) {
   330  	benchmark256Size(b, 8192, 8)
   331  }
   332  
   333  func BenchmarkHash8K_Tag128(b *testing.B) {
   334  	benchmark256Size(b, 8192, 16)
   335  }