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

     1  package zuc
     2  
     3  import (
     4  	"encoding/binary"
     5  	"encoding/hex"
     6  	"hash"
     7  	"testing"
     8  
     9  	"github.com/emmansun/gmsm/internal/cryptotest"
    10  )
    11  
    12  var key [16]byte
    13  var iv [16]byte
    14  
    15  var buf = make([]byte, 8192)
    16  
    17  func benchmarkSize(b *testing.B, size int) {
    18  	bench, _ := NewHash(key[:], iv[:])
    19  	b.SetBytes(int64(size))
    20  	sum := make([]byte, bench.Size())
    21  	for i := 0; i < b.N; i++ {
    22  		bench.Reset()
    23  		bench.Write(buf[:size])
    24  		bench.Sum(sum[:0])
    25  	}
    26  }
    27  
    28  func BenchmarkHash8Bytes(b *testing.B) {
    29  	benchmarkSize(b, 8)
    30  }
    31  
    32  func BenchmarkHash1K(b *testing.B) {
    33  	benchmarkSize(b, 1024)
    34  }
    35  
    36  func BenchmarkHash8K(b *testing.B) {
    37  	benchmarkSize(b, 8192)
    38  }
    39  
    40  var zucEIATests = []struct {
    41  	key       []byte
    42  	count     uint32
    43  	bearer    uint32
    44  	direction uint32
    45  	in        []uint32
    46  	nbits     int
    47  	mac       string
    48  }{
    49  	{
    50  		[]byte{
    51  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    52  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    53  		},
    54  		0,
    55  		0,
    56  		0,
    57  		[]uint32{0x00000000},
    58  		1,
    59  		"c8a9595e",
    60  	},
    61  	{
    62  		[]byte{
    63  			0xc9, 0xe6, 0xce, 0xc4, 0x60, 0x7c, 0x72, 0xdb,
    64  			0x00, 0x0a, 0xef, 0xa8, 0x83, 0x85, 0xab, 0x0a,
    65  		},
    66  		0xa94059da,
    67  		0x0a,
    68  		1,
    69  		[]uint32{
    70  			0x983b41d4, 0x7d780c9e, 0x1ad11d7e, 0xb70391b1,
    71  			0xde0b35da, 0x2dc62f83, 0xe7b78d63, 0x06ca0ea0,
    72  			0x7e941b7b, 0xe91348f9, 0xfcb170e2, 0x217fecd9,
    73  			0x7f9f68ad, 0xb16e5d7d, 0x21e569d2, 0x80ed775c,
    74  			0xebde3f40, 0x93c53881, 0x00000000,
    75  		},
    76  		0x241,
    77  		"fae8ff0b",
    78  	},
    79  	{
    80  		[]byte{
    81  			0x6b, 0x8b, 0x08, 0xee, 0x79, 0xe0, 0xb5, 0x98,
    82  			0x2d, 0x6d, 0x12, 0x8e, 0xa9, 0xf2, 0x20, 0xcb,
    83  		},
    84  		0x561eb2dd,
    85  		0x1c,
    86  		0,
    87  		[]uint32{
    88  			0x5bad7247, 0x10ba1c56, 0xd5a315f8, 0xd40f6e09,
    89  			0x3780be8e, 0x8de07b69, 0x92432018, 0xe08ed96a,
    90  			0x5734af8b, 0xad8a575d, 0x3a1f162f, 0x85045cc7,
    91  			0x70925571, 0xd9f5b94e, 0x454a77c1, 0x6e72936b,
    92  			0xf016ae15, 0x7499f054, 0x3b5d52ca, 0xa6dbeab6,
    93  			0x97d2bb73, 0xe41b8075, 0xdce79b4b, 0x86044f66,
    94  			0x1d4485a5, 0x43dd7860, 0x6e0419e8, 0x059859d3,
    95  			0xcb2b67ce, 0x0977603f, 0x81ff839e, 0x33185954,
    96  			0x4cfbc8d0, 0x0fef1a4c, 0x8510fb54, 0x7d6b06c6,
    97  			0x11ef44f1, 0xbce107cf, 0xa45a06aa, 0xb360152b,
    98  			0x28dc1ebe, 0x6f7fe09b, 0x0516f9a5, 0xb02a1bd8,
    99  			0x4bb0181e, 0x2e89e19b, 0xd8125930, 0xd178682f,
   100  			0x3862dc51, 0xb636f04e, 0x720c47c3, 0xce51ad70,
   101  			0xd94b9b22, 0x55fbae90, 0x6549f499, 0xf8c6d399,
   102  			0x47ed5e5d, 0xf8e2def1, 0x13253e7b, 0x08d0a76b,
   103  			0x6bfc68c8, 0x12f375c7, 0x9b8fe5fd, 0x85976aa6,
   104  			0xd46b4a23, 0x39d8ae51, 0x47f680fb, 0xe70f978b,
   105  			0x38effd7b, 0x2f7866a2, 0x2554e193, 0xa94e98a6,
   106  			0x8b74bd25, 0xbb2b3f5f, 0xb0a5fd59, 0x887f9ab6,
   107  			0x8159b717, 0x8d5b7b67, 0x7cb546bf, 0x41eadca2,
   108  			0x16fc1085, 0x0128f8bd, 0xef5c8d89, 0xf96afa4f,
   109  			0xa8b54885, 0x565ed838, 0xa950fee5, 0xf1c3b0a4,
   110  			0xf6fb71e5, 0x4dfd169e, 0x82cecc72, 0x66c850e6,
   111  			0x7c5ef0ba, 0x960f5214, 0x060e71eb, 0x172a75fc,
   112  			0x1486835c, 0xbea65344, 0x65b055c9, 0x6a72e410,
   113  			0x52241823, 0x25d83041, 0x4b40214d, 0xaa8091d2,
   114  			0xe0fb010a, 0xe15c6de9, 0x0850973b, 0xdf1e423b,
   115  			0xe148a237, 0xb87a0c9f, 0x34d4b476, 0x05b803d7,
   116  			0x43a86a90, 0x399a4af3, 0x96d3a120, 0x0a62f3d9,
   117  			0x507962e8, 0xe5bee6d3, 0xda2bb3f7, 0x237664ac,
   118  			0x7a292823, 0x900bc635, 0x03b29e80, 0xd63f6067,
   119  			0xbf8e1716, 0xac25beba, 0x350deb62, 0xa99fe031,
   120  			0x85eb4f69, 0x937ecd38, 0x7941fda5, 0x44ba67db,
   121  			0x09117749, 0x38b01827, 0xbcc69c92, 0xb3f772a9,
   122  			0xd2859ef0, 0x03398b1f, 0x6bbad7b5, 0x74f7989a,
   123  			0x1d10b2df, 0x798e0dbf, 0x30d65874, 0x64d24878,
   124  			0xcd00c0ea, 0xee8a1a0c, 0xc753a279, 0x79e11b41,
   125  			0xdb1de3d5, 0x038afaf4, 0x9f5c682c, 0x3748d8a3,
   126  			0xa9ec54e6, 0xa371275f, 0x1683510f, 0x8e4f9093,
   127  			0x8f9ab6e1, 0x34c2cfdf, 0x4841cba8, 0x8e0cff2b,
   128  			0x0bcc8e6a, 0xdcb71109, 0xb5198fec, 0xf1bb7e5c,
   129  			0x531aca50, 0xa56a8a3b, 0x6de59862, 0xd41fa113,
   130  			0xd9cd9578, 0x08f08571, 0xd9a4bb79, 0x2af271f6,
   131  			0xcc6dbb8d, 0xc7ec36e3, 0x6be1ed30, 0x8164c31c,
   132  			0x7c0afc54, 0x1c000000,
   133  		},
   134  		0x1626,
   135  		"0ca12792",
   136  	},
   137  }
   138  
   139  func TestEIA_Finish(t *testing.T) {
   140  	for i, test := range zucEIATests {
   141  		h, err := NewEIAHash(test.key, test.count, test.bearer, test.direction)
   142  		if err != nil {
   143  			t.Error(err)
   144  		}
   145  		in := make([]byte, len(test.in)*4)
   146  		for j, v := range test.in {
   147  			binary.BigEndian.PutUint32(in[j*4:], v)
   148  		}
   149  
   150  		mac := h.Finish(in, test.nbits)
   151  		if hex.EncodeToString(mac) != test.mac {
   152  			t.Errorf("case %d, expected=%s, result=%s\n", i+1, test.mac, hex.EncodeToString(mac))
   153  		}
   154  	}
   155  }
   156  
   157  func TestEIA_NewHash(t *testing.T) {
   158  	key := make([]byte, 16)
   159  	iv := make([]byte, 16)
   160  	_, err := NewHash(key[:1], iv)
   161  	if err == nil {
   162  		t.Fatal("error is expected")
   163  	}
   164  
   165  	_, err = NewHash(key, iv[:1])
   166  	if err == nil {
   167  		t.Fatal("error is expected")
   168  	}
   169  
   170  	h, err := NewHash(key, iv)
   171  	if err != nil {
   172  		t.Fatal(err)
   173  	}
   174  	if h.Size() != 4 {
   175  		t.Fatal("eia3 mac size should be 4 bytes")
   176  	}
   177  	if h.BlockSize() != 16 {
   178  		t.Fatal("current eia3 implementation's block size should be 16 bytes")
   179  	}
   180  
   181  }
   182  
   183  func TestEIA_Sum(t *testing.T) {
   184  	expected := "6c2db416"
   185  	h, err := NewEIAHash(zucEIATests[1].key, zucEIATests[1].count, zucEIATests[1].bearer, zucEIATests[1].direction)
   186  	if err != nil {
   187  		t.Fatal(err)
   188  	}
   189  	_, err = h.Write([]byte("emmansun"))
   190  	if err != nil {
   191  		t.Fatal(err)
   192  	}
   193  	_, err = h.Write([]byte("shangmi1"))
   194  	if err != nil {
   195  		t.Fatal(err)
   196  	}
   197  	_, err = h.Write([]byte("emmansun shangmi"))
   198  	if err != nil {
   199  		t.Fatal(err)
   200  	}
   201  	_, err = h.Write([]byte("emmansun shangmi 1234"))
   202  	if err != nil {
   203  		t.Fatal(err)
   204  	}
   205  	mac := h.Sum(nil)
   206  	if hex.EncodeToString(mac) != expected {
   207  		t.Errorf("expected=%s, result=%s\n", expected, hex.EncodeToString(mac))
   208  	}
   209  }
   210  
   211  func TestEIAHash(t *testing.T) {
   212  	t.Run("EIA-128", func(t *testing.T) {
   213  		cryptotest.TestHash(t, func() hash.Hash {
   214  			h, _ := NewEIAHash(zucEIATests[0].key, zucEIATests[0].count, zucEIATests[0].bearer, zucEIATests[0].direction)
   215  			return h
   216  		})
   217  	})
   218  }