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

     1  //go:build (amd64 || arm64 || ppc64 || ppc64le) && !purego
     2  
     3  package sm4
     4  
     5  import (
     6  	"crypto/cipher"
     7  
     8  	"github.com/emmansun/gmsm/internal/alias"
     9  )
    10  
    11  // Assert that sm4CipherAsm implements the ecbEncAble and ecbDecAble interfaces.
    12  var _ ecbEncAble = (*sm4CipherAsm)(nil)
    13  var _ ecbDecAble = (*sm4CipherAsm)(nil)
    14  
    15  const ecbEncrypt = 1
    16  const ecbDecrypt = 0
    17  
    18  type ecb struct {
    19  	b   *sm4CipherAsm
    20  	enc int
    21  }
    22  
    23  func (x *ecb) validate(dst, src []byte) {
    24  	if len(src)%BlockSize != 0 {
    25  		panic("cipher: input not full blocks")
    26  	}
    27  	if len(dst) < len(src) {
    28  		panic("cipher: output smaller than input")
    29  	}
    30  	if alias.InexactOverlap(dst[:len(src)], src) {
    31  		panic("cipher: invalid buffer overlap")
    32  	}
    33  }
    34  
    35  func (b *sm4CipherAsm) NewECBEncrypter() cipher.BlockMode {
    36  	var c ecb
    37  	c.b = b
    38  	c.enc = ecbEncrypt
    39  	return &c
    40  }
    41  
    42  func (b *sm4CipherAsm) NewECBDecrypter() cipher.BlockMode {
    43  	var c ecb
    44  	c.b = b
    45  	c.enc = ecbDecrypt
    46  	return &c
    47  }
    48  
    49  func (x *ecb) BlockSize() int { return BlockSize }
    50  
    51  //go:noescape
    52  func encryptSm4Ecb(xk *uint32, dst, src []byte)
    53  
    54  func (x *ecb) CryptBlocks(dst, src []byte) {
    55  	x.validate(dst, src)
    56  	if len(src) == 0 {
    57  		return
    58  	}
    59  	xk := &x.b.enc[0]
    60  	if x.enc == ecbDecrypt {
    61  		xk = &x.b.dec[0]
    62  	}
    63  	encryptSm4Ecb(xk, dst, src)
    64  }