This commit is contained in:
Sun Yimin 2024-12-04 18:54:19 +08:00 committed by GitHub
parent d009f7ebef
commit ddb5b69b53
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 26 additions and 5 deletions

View File

@ -78,17 +78,16 @@ func (c *eea) XORKeyStream(dst, src []byte) {
return return
} }
} }
words := (len(src) + WordSize - 1) / WordSize
rounds := words / RoundWords
var keyBytes [RoundBytes]byte var keyBytes [RoundBytes]byte
for i := 0; i < rounds; i++ { for len(src) >= RoundBytes {
genKeyStreamRev32(keyBytes[:], &c.zucState32) genKeyStreamRev32(keyBytes[:], &c.zucState32)
subtle.XORBytes(dst, src, keyBytes[:]) subtle.XORBytes(dst, src, keyBytes[:])
dst = dst[RoundBytes:] dst = dst[RoundBytes:]
src = src[RoundBytes:] src = src[RoundBytes:]
} }
if processedWords := rounds * RoundWords; processedWords < words { if len(src) > 0 {
byteLen := WordSize * (words - processedWords) words := (len(src) + WordSize - 1) / WordSize
byteLen := WordSize * words
genKeyStreamRev32(keyBytes[:byteLen], &c.zucState32) genKeyStreamRev32(keyBytes[:byteLen], &c.zucState32)
n := subtle.XORBytes(dst, src, keyBytes[:]) n := subtle.XORBytes(dst, src, keyBytes[:])
// save remaining key bytes // save remaining key bytes

View File

@ -160,6 +160,28 @@ func TestXORStreamAt(t *testing.T) {
}) })
} }
func TestIssue284(t *testing.T) {
key, err := hex.DecodeString(zucEEATests[0].key)
if err != nil {
t.Error(err)
}
c, err := NewEEACipher(key, zucEEATests[0].count, zucEEATests[0].bearer, zucEEATests[0].direction)
if err != nil {
t.Error(err)
}
src := make([]byte, 200)
expected := make([]byte, 200)
dst := make([]byte, 200)
c.XORKeyStream(expected, src)
for i := 124; i <= 200; i++ {
c.XORKeyStreamAt(dst, src[:i], 0)
if !bytes.Equal(expected[:i], dst[:i]) {
t.Fatalf("failed for len %v", i)
}
}
}
func benchmarkStream(b *testing.B, buf []byte) { func benchmarkStream(b *testing.B, buf []byte) {
b.SetBytes(int64(len(buf))) b.SetBytes(int64(len(buf)))