gmsm/sm4/cbc_cipher_asm.go

113 lines
2.6 KiB
Go
Raw Normal View History

//go:build (amd64 && !purego) || (arm64 && !purego)
// +build amd64,!purego arm64,!purego
2022-01-21 11:24:10 +08:00
package sm4
import (
"crypto/cipher"
2022-08-18 14:49:35 +08:00
"github.com/emmansun/gmsm/internal/alias"
2022-01-21 11:24:10 +08:00
)
2022-07-26 08:30:24 +08:00
// Assert that sm4CipherAsm implements the cbcEncAble and cbcDecAble interfaces.
var _ cbcEncAble = (*sm4CipherAsm)(nil)
var _ cbcDecAble = (*sm4CipherAsm)(nil)
const cbcEncrypt = 1
const cbcDecrypt = 0
2022-01-21 11:24:10 +08:00
type cbc struct {
b *sm4CipherAsm
iv []byte
tmp []byte
enc int
}
func (b *sm4CipherAsm) NewCBCEncrypter(iv []byte) cipher.BlockMode {
var c cbc
c.b = b
c.enc = cbcEncrypt
c.iv = make([]byte, BlockSize)
c.tmp = make([]byte, BlockSize)
copy(c.iv, iv)
return &c
2022-01-21 11:24:10 +08:00
}
func (b *sm4CipherAsm) NewCBCDecrypter(iv []byte) cipher.BlockMode {
var c cbc
c.b = b
c.enc = cbcDecrypt
2022-01-21 11:24:10 +08:00
c.iv = make([]byte, BlockSize)
c.tmp = make([]byte, BlockSize)
copy(c.iv, iv)
return &c
}
func (x *cbc) BlockSize() int { return BlockSize }
//go:noescape
func encryptBlocksChain(xk *uint32, dst, src []byte, iv *byte)
//go:noescape
func decryptBlocksChain(xk *uint32, dst, src []byte, iv *byte)
2022-01-21 11:24:10 +08:00
func (x *cbc) CryptBlocks(dst, src []byte) {
if len(src)%BlockSize != 0 {
panic("cipher: input not full blocks")
}
if len(dst) < len(src) {
panic("cipher: output smaller than input")
}
2022-08-18 14:49:35 +08:00
if alias.InexactOverlap(dst[:len(src)], src) {
2022-01-21 11:24:10 +08:00
panic("cipher: invalid buffer overlap")
}
if len(src) == 0 {
return
}
if x.enc == cbcEncrypt {
encryptBlocksChain(&x.b.enc[0], dst, src, &x.iv[0])
return
2022-01-21 11:24:10 +08:00
}
2022-07-20 11:43:49 +08:00
// For each block, we need to xor the decrypted data with the previous block's ciphertext (the iv).
// To avoid making a copy each time, we loop over the blocks BACKWARDS.
2022-01-21 11:24:10 +08:00
end := len(src)
2022-07-20 11:43:49 +08:00
// Copy the last block of ciphertext in preparation as the new iv.
2022-01-21 11:24:10 +08:00
copy(x.tmp, src[end-BlockSize:end])
2022-07-20 11:43:49 +08:00
2023-08-03 15:17:01 +08:00
2022-07-20 11:43:49 +08:00
decKeyPtr := &x.b.dec[0]
2023-08-03 15:17:01 +08:00
start := end - 2*x.b.blocksSize
for start > 0 {
decryptBlocksChain(decKeyPtr, dst[start:end], src[start:end], &src[start-BlockSize])
end = start
start -= 2*x.b.blocksSize
}
start = end - x.b.blocksSize
2022-01-21 11:24:10 +08:00
for start > 0 {
decryptBlocksChain(decKeyPtr, dst[start:end], src[start:end], &src[start-BlockSize])
2022-01-21 11:24:10 +08:00
end = start
start -= x.b.blocksSize
}
2022-07-20 11:43:49 +08:00
// Handle remain first blocks
2023-08-03 15:17:01 +08:00
var temp []byte = make([]byte, x.b.blocksSize)
var batchSrc []byte = make([]byte, x.b.blocksSize+BlockSize)
2022-07-20 11:43:49 +08:00
copy(batchSrc, x.iv)
copy(batchSrc[BlockSize:], src[:end])
decryptBlocksChain(decKeyPtr, temp, batchSrc[BlockSize:], &batchSrc[0])
copy(dst, temp[:end])
2022-07-20 11:43:49 +08:00
2022-01-21 11:24:10 +08:00
// Set the new iv to the first block we copied earlier.
x.iv, x.tmp = x.tmp, x.iv
}
func (x *cbc) SetIV(iv []byte) {
if len(iv) != BlockSize {
panic("cipher: incorrect length IV")
}
copy(x.iv[:], iv)
}