From 2445ff3f71f174d7cb1a774280d26d6c69211618 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=E7=AB=99=E8=B4=B4=E5=90=A7=E8=9C=A1=E6=B2=B9?= Date: Sun, 10 Mar 2024 00:11:06 +0800 Subject: [PATCH] Fix CFB8 non-overlapping decrypt (#275) * Fix CFB8 non-overlapping decrypt * Fix CFB8 edge case of empty src --- net/CFB8/cfb8.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/net/CFB8/cfb8.go b/net/CFB8/cfb8.go index 26e0cbf..8229468 100644 --- a/net/CFB8/cfb8.go +++ b/net/CFB8/cfb8.go @@ -35,6 +35,9 @@ func newCFB8(c cipher.Block, iv []byte, de bool) *CFB8 { } func (cf *CFB8) XORKeyStream(dst, src []byte) { + if len(src) == 0 { + return + } if len(dst) < len(src) { panic("cfb8: output smaller than input") } @@ -65,7 +68,9 @@ func (cf *CFB8) XORKeyStream(dst, src []byte) { val byte ) 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 { cf.c.Encrypt(dst[i:], ciphertext[i:]) }