Fix CFB8 non-overlapping decrypt (#275)

* Fix CFB8 non-overlapping decrypt

* Fix CFB8 edge case of empty src
This commit is contained in:
B站贴吧蜡油
2024-03-10 00:11:06 +08:00
committed by GitHub
parent bc3d77d784
commit 2445ff3f71

View File

@ -35,6 +35,9 @@ func newCFB8(c cipher.Block, iv []byte, de bool) *CFB8 {
} }
func (cf *CFB8) XORKeyStream(dst, src []byte) { func (cf *CFB8) XORKeyStream(dst, src []byte) {
if len(src) == 0 {
return
}
if len(dst) < len(src) { if len(dst) < len(src) {
panic("cfb8: output smaller than input") panic("cfb8: output smaller than input")
} }
@ -65,7 +68,9 @@ func (cf *CFB8) XORKeyStream(dst, src []byte) {
val byte val byte
) )
dst = dst[:len(src)] dst = dst[:len(src)]
if cf.de { if cf.de && // and requires to be non-overlapping at all
uintptr(unsafe.Pointer(&dst[0])) <= uintptr(unsafe.Pointer(&src[len(src)-1])) &&
uintptr(unsafe.Pointer(&src[0])) <= uintptr(unsafe.Pointer(&dst[len(dst)-1])) {
for i = 0; i < len(src)-cf.blockSize; i += 1 { for i = 0; i < len(src)-cf.blockSize; i += 1 {
cf.c.Encrypt(dst[i:], ciphertext[i:]) cf.c.Encrypt(dst[i:], ciphertext[i:])
} }