github.com/emmansun/gmsm@v0.29.1/cipher/hctr_test.go (about)

     1  package cipher_test
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/hex"
     6  	"testing"
     7  
     8  	"github.com/emmansun/gmsm/cipher"
     9  	"github.com/emmansun/gmsm/sm4"
    10  )
    11  
    12  var hctrSM4TestVectors = []struct {
    13  	key        string
    14  	hashKey    string
    15  	tweak      string
    16  	plaintext  string
    17  	ciphertext string
    18  }{
    19  	{
    20  		"2B7E151628AED2A6ABF7158809CF4F3C",
    21  		"000102030405060708090A0B0C0D0E0F",
    22  		"F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF",
    23  		"6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c37106bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c37106bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710",
    24  		"8858dda3034233e377936b76ce7edeb6a245075a37800b0b996e8e974c9032ac8de40d90ee4ee5fb58bc10cbc95779485ab38ffb0b4f961d85f086db705ff723edbeaec649b3b406b11b96a418a9c2c51ef41cdd24e472c18336e9efcd07b7e264a1e2d46615198eb74938d72104fa89294a6360cdb6b032a704cf07a087bb2283598552701b2f710d6528d9c3f4dab529afef4413f25169b6cbf8168ccbfa02a2f507513d0cb3802da34dbd928b67e6afc30ca91011070cfd40c2ef3d4ac041",
    25  	},
    26  	{
    27  		"2B7E151628AED2A6ABF7158809CF4F3C",
    28  		"000102030405060708090A0B0C0D0E0F",
    29  		"F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF",
    30  		"6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710",
    31  		"9cd7481d3b7ca904b14b4084d9d4c83ed39eac8e16747895fc2ae1eecd220276af3d0d2f21cb3807561347c81ad138117dd85c652afe16a47dc68eb884068ae3",
    32  	},
    33  	{
    34  		"2B7E151628AED2A6ABF7158809CF4F3C",
    35  		"000102030405060708090A0B0C0D0E0F",
    36  		"F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF",
    37  		"6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417b",
    38  		"f7505aff357ac13107cdb2848c6bb2dcdda473f7a6ea939d44f52c986c11ca9341042f2b0091a1ca5c8f708cae8ca6a5c59e2228b3616c4455627722",
    39  	},
    40  	{
    41  		"2B7E151628AED2A6ABF7158809CF4F3C",
    42  		"000102030405060708090A0B0C0D0E0F",
    43  		"F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF",
    44  		"6bc1bee22e409f96e93d7e117393172a",
    45  		"b7b1dd75f608012dc69621d4ea720a60",
    46  	},
    47  }
    48  
    49  func TestHCTR(t *testing.T) {
    50  	for i, test := range hctrSM4TestVectors {
    51  		key1, _ := hex.DecodeString(test.key)
    52  		key2, _ := hex.DecodeString(test.hashKey)
    53  		tw, _ := hex.DecodeString(test.tweak)
    54  		plaintext, _ := hex.DecodeString(test.plaintext)
    55  		ciphertext, _ := hex.DecodeString(test.ciphertext)
    56  		got := make([]byte, len(plaintext))
    57  		c, err := sm4.NewCipher(key1)
    58  		if err != nil {
    59  			t.Fatal(err)
    60  		}
    61  		hctr, err := cipher.NewHCTR(c, tw, key2)
    62  		if err != nil {
    63  			t.Fatal(err)
    64  		}
    65  		hctr.EncryptBytes(got, plaintext)
    66  		if !bytes.Equal(got, ciphertext) {
    67  			t.Fatalf("%v case encrypt failed, got %x\n", i+1, got)
    68  		}
    69  
    70  		hctr.DecryptBytes(got, ciphertext)
    71  		if !bytes.Equal(got, plaintext) {
    72  			t.Fatalf("%v case decrypt failed, got %x\n", i+1, got)
    73  		}
    74  	}
    75  }