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

     1  package zuc
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func Test_genKeyword_case1(t *testing.T) {
     9  	s, _ := newZUCState([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})
    10  	z1 := s.genKeyword()
    11  	if z1 != 0x27bede74 {
    12  		t.Errorf("expected=%x, result=%x\n", 0x27bede74, z1)
    13  	}
    14  	if s.r1 != 0xc7ee7f13 {
    15  		t.Errorf("expected=%x, result=%x\n", 0xc7ee7f13, s.r1)
    16  	}
    17  	if s.r2 != 0xc0fa817 {
    18  		t.Errorf("expected=%x, result=%x\n", 0xc0fa817, s.r2)
    19  	}	
    20  	z2 := s.genKeyword()
    21  	if z2 != 0x018082da {
    22  		t.Errorf("expected=%x, result=%x\n", 0x018082da, z2)
    23  	}
    24  }
    25  
    26  func Test_genKeyword_case2(t *testing.T) {
    27  	s, _ := newZUCState([]byte{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, []byte{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255})
    28  	z1 := s.genKeyword()
    29  	if z1 != 0x0657cfa0 {
    30  		t.Errorf("expected=%x, result=%x\n", 0x0657cfa0, z1)
    31  	}
    32  	z2 := s.genKeyword()
    33  	if z2 != 0x7096398b {
    34  		t.Errorf("expected=%x, result=%x\n", 0x7096398b, z2)
    35  	}
    36  }
    37  
    38  func Test_genKeyword_case3(t *testing.T) {
    39  	s, _ := newZUCState([]byte{0x3d, 0x4c, 0x4b, 0xe9, 0x6a, 0x82, 0xfd, 0xae, 0xb5, 0x8f, 0x64, 0x1d, 0xb1, 0x7b, 0x45, 0x5b}, []byte{0x84, 0x31, 0x9a, 0xa8, 0xde, 0x69, 0x15, 0xca, 0x1f, 0x6b, 0xda, 0x6b, 0xfb, 0xd8, 0xc7, 0x66})
    40  	z1 := s.genKeyword()
    41  	if z1 != 0x14f1c272 {
    42  		t.Errorf("expected=%x, result=%x\n", 0x14f1c272, z1)
    43  	}
    44  	z2 := s.genKeyword()
    45  	if z2 != 0x3279c419 {
    46  		t.Errorf("expected=%x, result=%x\n", 0x3279c419, z2)
    47  	}
    48  }
    49  
    50  var zuc256Tests = []struct {
    51  	key          []byte
    52  	iv           []byte
    53  	expectedKeys []uint32
    54  }{
    55  	{
    56  		[]byte{
    57  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    58  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    59  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    60  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    61  		},
    62  		[]byte{
    63  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    64  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    65  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    66  		},
    67  		[]uint32{
    68  			0x58d03ad6, 0x2e032ce2, 0xdafc683a, 0x39bdcb03, 0x52a2bc67,
    69  			0xf1b7de74, 0x163ce3a1, 0x01ef5558, 0x9639d75b, 0x95fa681b,
    70  			0x7f090df7, 0x56391ccc, 0x903b7612, 0x744d544c, 0x17bc3fad,
    71  			0x8b163b08, 0x21787c0b, 0x97775bb8, 0x4943c6bb, 0xe8ad8afd,
    72  		},
    73  	},
    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, 0xff,
    79  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    80  		},
    81  		[]byte{
    82  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    83  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    84  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    85  		},
    86  		[]uint32{
    87  			0x3356cbae, 0xd1a1c18b, 0x6baa4ffe, 0x343f777c, 0x9e15128f,
    88  			0x251ab65b, 0x949f7b26, 0xef7157f2, 0x96dd2fa9, 0xdf95e3ee,
    89  			0x7a5be02e, 0xc32ba585, 0x505af316, 0xc2f9ded2, 0x7cdbd935,
    90  			0xe441ce11, 0x15fd0a80, 0xbb7aef67, 0x68989416, 0xb8fac8c2,
    91  		},
    92  	},
    93  }
    94  
    95  func Test_ZUC256(t *testing.T) {
    96  	for i, test := range zuc256Tests {
    97  		c, err := newZUCState(test.key, test.iv)
    98  		if err != nil {
    99  			t.Error(err)
   100  		}
   101  		words := make([]uint32, 20)
   102  		c.genKeywords(words)
   103  		if !reflect.DeepEqual(words, test.expectedKeys) {
   104  			t.Errorf("#%d: wrong output: got %x, expected %x", i, words, test.expectedKeys)
   105  		}
   106  	}
   107  }