github.com/emmansun/gmsm@v0.29.1/sm4/cipher_asm_test.go (about)

     1  //go:build (amd64 || arm64 || ppc64 || ppc64le) && !purego
     2  
     3  package sm4
     4  
     5  import (
     6  	"bytes"
     7  	"crypto/cipher"
     8  	"testing"
     9  )
    10  
    11  func TestWithoutGFMUL(t *testing.T) {
    12  	key := make([]byte, 16)
    13  	src := make([]byte, 16)
    14  	var dst []byte
    15  	var nonce [12]byte
    16  	var c cipher.Block
    17  	var err error
    18  
    19  	if supportSM4 {
    20  		c, err = newCipherNI(key)
    21  	} else if !supportsAES {
    22  		c, err = newCipherGeneric(key)
    23  	} else {
    24  		blocks := 4
    25  		if useAVX2 {
    26  			blocks = 8
    27  		}
    28  		c1 := &sm4CipherAsm{sm4Cipher{}, blocks, blocks * BlockSize}
    29  		expandKeyAsm(&key[0], &ck[0], &c1.enc[0], &c1.dec[0], INST_AES)
    30  		c = c1
    31  	}
    32  	if err != nil {
    33  		t.Fatal(err)
    34  	}
    35  
    36  	var sm4gcm cipher.AEAD
    37  	sm4gcm, err = cipher.NewGCM(c)
    38  	if err != nil {
    39  		t.Fatal(err)
    40  	}
    41  	dst = sm4gcm.Seal(nil, nonce[:], src, nil)
    42  	src, err = sm4gcm.Open(nil, nonce[:], dst, nil)
    43  	if err != nil {
    44  		t.Fatal(err)
    45  	}
    46  	if !bytes.Equal(key, src) {
    47  		t.Errorf("bad encryption")
    48  	}
    49  }
    50  
    51  func TestEncryptBlockAsm(t *testing.T) {
    52  	src := []byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10}
    53  	expected := []byte{0x68, 0x1e, 0xdf, 0x34, 0xd2, 0x06, 0x96, 0x5e, 0x86, 0xb3, 0xe9, 0x4f, 0x53, 0x6e, 0x42, 0x46}
    54  	encRes2 := make([]uint32, 32)
    55  	decRes2 := make([]uint32, 32)
    56  	expandKeyAsm(&src[0], &ck[0], &encRes2[0], &decRes2[0], 0)
    57  	dst := make([]byte, 16)
    58  	encryptBlockAsm(&encRes2[0], &dst[0], &src[0], 0)
    59  	if !bytes.Equal(dst, expected) {
    60  		t.Errorf("expected=%x, result=%x\n", expected, dst)
    61  	}
    62  	encryptBlockAsm(&decRes2[0], &dst[0], &expected[0], 0)
    63  	if !bytes.Equal(dst, src) {
    64  		t.Errorf("expected=%x, result=%x\n", src, dst)
    65  	}
    66  }
    67  
    68  func TestEncryptBlocksWithAESNI(t *testing.T) {
    69  	if !supportsAES {
    70  		t.Skip("AES-NI not available")
    71  	}
    72  
    73  	blocks := 4
    74  	if useAVX2 {
    75  		blocks = 8
    76  	}
    77  
    78  	src := make([]byte, 16*blocks)
    79  	expected := make([]byte, 16*blocks)
    80  	key := []byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10}
    81  	for i := 0; i < blocks; i++ {
    82  		copy(src[i*16:], key)
    83  		copy(expected[i*16:], []byte{0x68, 0x1e, 0xdf, 0x34, 0xd2, 0x06, 0x96, 0x5e, 0x86, 0xb3, 0xe9, 0x4f, 0x53, 0x6e, 0x42, 0x46})
    84  	}
    85  
    86  	c := &sm4CipherAsm{sm4Cipher{}, blocks, blocks * BlockSize}
    87  	expandKeyAsm(&key[0], &ck[0], &c.enc[0], &c.dec[0], INST_AES)
    88  	dst := make([]byte, 16*blocks)
    89  
    90  	c.EncryptBlocks(dst, src)
    91  	if !bytes.Equal(dst, expected) {
    92  		t.Errorf("expected=%x, result=%x\n", expected, dst)
    93  	}
    94  
    95  	c.DecryptBlocks(dst, expected)
    96  	if !bytes.Equal(dst, src) {
    97  		t.Errorf("expected=%x, result=%x\n", src, dst)
    98  	}
    99  }
   100  
   101  func TestEncryptBlocksDoubleWithAESNI(t *testing.T) {
   102  	if !supportsAES {
   103  		t.Skip("AES-NI not available")
   104  	}
   105  
   106  	blocks := 4
   107  	if useAVX2 {
   108  		blocks = 8
   109  	}
   110  
   111  	src := make([]byte, 2*16*blocks)
   112  	expected := make([]byte, 2*16*blocks)
   113  	key := []byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10}
   114  	for i := 0; i < 2*blocks; i++ {
   115  		copy(src[i*16:], key)
   116  		copy(expected[i*16:], []byte{0x68, 0x1e, 0xdf, 0x34, 0xd2, 0x06, 0x96, 0x5e, 0x86, 0xb3, 0xe9, 0x4f, 0x53, 0x6e, 0x42, 0x46})
   117  	}
   118  
   119  	c := &sm4CipherAsm{sm4Cipher{}, blocks, blocks * BlockSize}
   120  	expandKeyAsm(&key[0], &ck[0], &c.enc[0], &c.dec[0], INST_AES)
   121  	dst := make([]byte, 2*16*blocks)
   122  
   123  	c.EncryptBlocks(dst, src)
   124  	if !bytes.Equal(dst, expected) {
   125  		t.Errorf("expected=%x, result=%x\n", expected, dst)
   126  	}
   127  
   128  	c.DecryptBlocks(dst, expected)
   129  	if !bytes.Equal(dst, src) {
   130  		t.Errorf("expected=%x, result=%x\n", src, dst)
   131  	}
   132  }
   133  
   134  func BenchmarkExpandAESNI(b *testing.B) {
   135  	c := &sm4Cipher{}
   136  	b.ResetTimer()
   137  	for i := 0; i < b.N; i++ {
   138  		expandKeyAsm(&encryptTests[0].key[0], &ck[0], &c.enc[0], &c.dec[0], INST_AES)
   139  	}
   140  }
   141  
   142  func BenchmarkEncryptAsm(b *testing.B) {
   143  	src := []byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10}
   144  	encRes2 := make([]uint32, 32)
   145  	decRes2 := make([]uint32, 32)
   146  	expandKeyAsm(&src[0], &ck[0], &encRes2[0], &decRes2[0], 0)
   147  	dst := make([]byte, 16)
   148  	b.SetBytes(int64(len(src)))
   149  	b.ResetTimer()
   150  	for i := 0; i < b.N; i++ {
   151  		encryptBlockAsm(&encRes2[0], &dst[0], &src[0], 0)
   152  	}
   153  }