Skip to content

Commit

Permalink
bwtester: fix incompatibility in PRG packet data
Browse files Browse the repository at this point in the history
In #209, we introduced a reusable internal buffer for filling the
packets with PRG data. This buffer was not fully reset for each packet,
resulting in a different output and thus an incompatibility with the
older implementation. Only for the first packet, we would get the
"correct" result, all subsequent packets are rejected.

Fix by resetting buffer on each Fill call.

Looking at this code, I've noticed that the packet data is not actually
filled with PRG data. Only the first 16 bytes, plus a few bytes at the
end, are ever filled with the PRG output. We should just simplify this
anyway.
  • Loading branch information
matzf committed Jan 7, 2022
1 parent 00775c2 commit 65835ce
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion bwtester/bwtest/bwtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,12 @@ func newPrgFiller(key []byte) *prgFiller {
// Fill the buffer with AES PRG in counter mode
// The value of the ith 16-byte block is simply an encryption of i under the key
func (f *prgFiller) Fill(iv int, data []byte) {
memzero(f.buf)
i := uint32(iv)
j := 0
for j <= len(data)-aes.BlockSize {
binary.LittleEndian.PutUint32(f.buf, i)
f.aes.Encrypt(data, f.buf)
f.aes.Encrypt(data, f.buf) // BUG(matzf): this should be data[j:]! data is mostly left zero.
j = j + aes.BlockSize
i = i + uint32(aes.BlockSize)
}
Expand All @@ -105,6 +106,12 @@ func (f *prgFiller) Fill(iv int, data []byte) {
}
}

func memzero(buf []byte) {
for i := range buf {
buf[i] = 0
}
}

// Encode Result into a sufficiently large byte buffer that is passed in, return the number of bytes written
func EncodeResult(res Result, buf []byte) (int, error) {
var bb bytes.Buffer
Expand Down

0 comments on commit 65835ce

Please sign in to comment.