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

     1  package cipher_test
     2  
     3  import (
     4  	"bytes"
     5  	"crypto/rand"
     6  	"io"
     7  	"testing"
     8  
     9  	"github.com/emmansun/gmsm/cipher"
    10  	"github.com/emmansun/gmsm/sm4"
    11  )
    12  
    13  var ecbSM4Tests = []struct {
    14  	name string
    15  	key  []byte
    16  	in   []byte
    17  }{
    18  	{
    19  		"1 block",
    20  		[]byte("0123456789ABCDEF"),
    21  		[]byte("exampleplaintext"),
    22  	},
    23  	{
    24  		"2 same blocks",
    25  		[]byte("0123456789ABCDEF"),
    26  		[]byte("exampleplaintextexampleplaintext"),
    27  	},
    28  	{
    29  		"2 different blocks",
    30  		[]byte("0123456789ABCDEF"),
    31  		[]byte("exampleplaintextfedcba9876543210"),
    32  	},
    33  	{
    34  		"3 same blocks",
    35  		[]byte("0123456789ABCDEF"),
    36  		[]byte("exampleplaintextexampleplaintextexampleplaintext"),
    37  	},
    38  	{
    39  		"4 same blocks",
    40  		[]byte("0123456789ABCDEF"),
    41  		[]byte("exampleplaintextexampleplaintextexampleplaintextexampleplaintext"),
    42  	},
    43  	{
    44  		"5 same blocks",
    45  		[]byte("0123456789ABCDEF"),
    46  		[]byte("exampleplaintextexampleplaintextexampleplaintextexampleplaintextexampleplaintext"),
    47  	},
    48  	{
    49  		"6 same blocks",
    50  		[]byte("0123456789ABCDEF"),
    51  		[]byte("exampleplaintextexampleplaintextexampleplaintextexampleplaintextexampleplaintextexampleplaintext"),
    52  	},
    53  	{
    54  		"7 same blocks",
    55  		[]byte("0123456789ABCDEF"),
    56  		[]byte("exampleplaintextexampleplaintextexampleplaintextexampleplaintextexampleplaintextexampleplaintextexampleplaintext"),
    57  	},
    58  	{
    59  		"8 same blocks",
    60  		[]byte("0123456789ABCDEF"),
    61  		[]byte("exampleplaintextexampleplaintextexampleplaintextexampleplaintextexampleplaintextexampleplaintextexampleplaintextexampleplaintext"),
    62  	},
    63  	{
    64  		"9 same blocks",
    65  		[]byte("0123456789ABCDEF"),
    66  		[]byte("exampleplaintextexampleplaintextexampleplaintextexampleplaintextexampleplaintextexampleplaintextexampleplaintextexampleplaintextexampleplaintext"),
    67  	},
    68  	{
    69  		"18 same blocks",
    70  		[]byte("0123456789ABCDEF"),
    71  		[]byte("exampleplaintextexampleplaintextexampleplaintextexampleplaintextexampleplaintextexampleplaintextexampleplaintextexampleplaintextexampleplaintextexampleplaintextexampleplaintextexampleplaintextexampleplaintextexampleplaintextexampleplaintextexampleplaintextexampleplaintextexampleplaintext"),
    72  	},
    73  }
    74  
    75  func TestECBBasic(t *testing.T) {
    76  	for _, test := range ecbSM4Tests {
    77  		c, err := sm4.NewCipher(test.key)
    78  		if err != nil {
    79  			t.Errorf("%s: NewCipher(%d bytes) = %s", test.name, len(test.key), err)
    80  			continue
    81  		}
    82  		encrypter := cipher.NewECBEncrypter(c)
    83  		ciphertext := make([]byte, len(test.in))
    84  		encrypter.CryptBlocks(ciphertext, test.in)
    85  
    86  		plaintext := make([]byte, len(test.in))
    87  		decrypter := cipher.NewECBDecrypter(c)
    88  		decrypter.CryptBlocks(plaintext, ciphertext)
    89  		if !bytes.Equal(test.in, plaintext) {
    90  			t.Errorf("%s: ECB encrypt/decrypt failed, %s", test.name, string(plaintext))
    91  		}
    92  	}
    93  }
    94  
    95  func TestECBRandom(t *testing.T) {
    96  	key := []byte("0123456789ABCDEF")
    97  	plaintext := make([]byte, 464)
    98  	ciphertext := make([]byte, 464)
    99  	io.ReadFull(rand.Reader, plaintext)
   100  	c, err := sm4.NewCipher(key)
   101  	if err != nil {
   102  		t.Fatal(err)
   103  	}
   104  	encrypter := cipher.NewECBEncrypter(c)
   105  	encrypter.CryptBlocks(ciphertext, plaintext)
   106  	result := make([]byte, 464)
   107  	decrypter := cipher.NewECBDecrypter(c)
   108  	decrypter.CryptBlocks(result, ciphertext)
   109  	if !bytes.Equal(result, plaintext) {
   110  		t.Error("ECB encrypt/decrypt failed")
   111  	}
   112  }
   113  
   114  func shouldPanic(t *testing.T, f func()) {
   115  	t.Helper()
   116  	defer func() { _ = recover() }()
   117  	f()
   118  	t.Errorf("should have panicked")
   119  }
   120  
   121  func TestECBValidate(t *testing.T) {
   122  	key := make([]byte, 16)
   123  	src := make([]byte, 32)
   124  	c, err := sm4.NewCipher(key)
   125  	if err != nil {
   126  		t.Fatal(err)
   127  	}
   128  
   129  	decrypter := cipher.NewECBDecrypter(c)
   130  	// test len(src) == 0
   131  	decrypter.CryptBlocks(nil, nil)
   132  
   133  	// cipher: input not full blocks
   134  	shouldPanic(t, func() {
   135  		decrypter.CryptBlocks(src, src[1:])
   136  	})
   137  	// cipher: output smaller than input
   138  	shouldPanic(t, func() {
   139  		decrypter.CryptBlocks(src[1:], src)
   140  	})
   141  	// cipher: invalid buffer overlap
   142  	shouldPanic(t, func() {
   143  		decrypter.CryptBlocks(src[1:17], src[2:18])
   144  	})
   145  }