Skip to content

Commit

Permalink
fix #284
Browse files Browse the repository at this point in the history
  • Loading branch information
emmansun authored Dec 4, 2024
1 parent d009f7e commit ddb5b69
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
9 changes: 4 additions & 5 deletions zuc/eea.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,16 @@ func (c *eea) XORKeyStream(dst, src []byte) {
return
}
}
words := (len(src) + WordSize - 1) / WordSize
rounds := words / RoundWords
var keyBytes [RoundBytes]byte
for i := 0; i < rounds; i++ {
for len(src) >= RoundBytes {
genKeyStreamRev32(keyBytes[:], &c.zucState32)
subtle.XORBytes(dst, src, keyBytes[:])
dst = dst[RoundBytes:]
src = src[RoundBytes:]
}
if processedWords := rounds * RoundWords; processedWords < words {
byteLen := WordSize * (words - processedWords)
if len(src) > 0 {
words := (len(src) + WordSize - 1) / WordSize
byteLen := WordSize * words
genKeyStreamRev32(keyBytes[:byteLen], &c.zucState32)
n := subtle.XORBytes(dst, src, keyBytes[:])
// save remaining key bytes
Expand Down
22 changes: 22 additions & 0 deletions zuc/eea_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
b.SetBytes(int64(len(buf)))

Expand Down

0 comments on commit ddb5b69

Please sign in to comment.