mirror of
https://github.com/emmansun/gmsm.git
synced 2025-04-27 04:36:19 +08:00
fix #284
This commit is contained in:
parent
d009f7ebef
commit
ddb5b69b53
@ -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
|
||||||
|
@ -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)))
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user