sm4: fallback ctr change

This commit is contained in:
Sun Yimin 2025-02-26 10:24:46 +08:00 committed by GitHub
parent 27e7ceacbc
commit 8a25134c82
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -34,17 +34,18 @@ func (c *sm4CipherAsm) NewCTR(iv []byte) cipher.Stream {
} }
s := &ctr{ s := &ctr{
b: c, b: c,
ctr: bytes.Clone(iv), ctr: make([]byte, c.blocksSize),
out: make([]byte, 0, bufSize), out: make([]byte, 0, bufSize),
outUsed: 0, outUsed: 0,
} }
copy(s.ctr, iv)
for i := 1; i < c.batchBlocks; i++ { for i := 1; i < c.batchBlocks; i++ {
s.genCtr(i * BlockSize) s.genCtr(i * BlockSize)
} }
return s return s
} }
func (x *ctr) genCtr(start int) { func (x *ctr) genCtr(start int) {
if start >= BlockSize { if start >= BlockSize {
copy(x.ctr[start:], x.ctr[start-BlockSize:start]) copy(x.ctr[start:], x.ctr[start-BlockSize:start])