replay: minor API changes to more idiomatic Go

Signed-off-by: Riobard Zhan <me@riobard.com>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Riobard Zhan 2020-09-10 02:06:44 +08:00 committed by Jason A. Donenfeld
parent 22af3890f6
commit 2c143dce0f
4 changed files with 17 additions and 17 deletions

View file

@ -26,7 +26,7 @@ type Keypair struct {
sendNonce uint64 sendNonce uint64
send cipher.AEAD send cipher.AEAD
receive cipher.AEAD receive cipher.AEAD
replayFilter replay.ReplayFilter replayFilter replay.Filter
isInitiator bool isInitiator bool
created time.Time created time.Time
localIndex uint32 localIndex uint32

View file

@ -567,7 +567,7 @@ func (peer *Peer) BeginSymmetricSession() error {
keypair.created = time.Now() keypair.created = time.Now()
keypair.sendNonce = 0 keypair.sendNonce = 0
keypair.replayFilter.Init() keypair.replayFilter.Reset()
keypair.isInitiator = isInitiator keypair.isInitiator = isInitiator
keypair.localIndex = peer.handshake.localIndex keypair.localIndex = peer.handshake.localIndex
keypair.remoteIndex = peer.handshake.remoteIndex keypair.remoteIndex = peer.handshake.remoteIndex

View file

@ -17,24 +17,24 @@ const (
bitMask = blockBits - 1 bitMask = blockBits - 1
) )
// A ReplayFilter rejects replayed messages by checking if message counter value is // A Filter rejects replayed messages by checking if message counter value is
// within a sliding window of previously received messages. // within a sliding window of previously received messages.
// The zero value for ReplayFilter is an empty filter ready to use. // The zero value for Filter is an empty filter ready to use.
// Filters are unsafe for concurrent use. // Filters are unsafe for concurrent use.
type ReplayFilter struct { type Filter struct {
last uint64 last uint64
ring [ringBlocks]block ring [ringBlocks]block
} }
// Init resets the filter to empty state. // Reset resets the filter to empty state.
func (f *ReplayFilter) Init() { func (f *Filter) Reset() {
f.last = 0 f.last = 0
f.ring[0] = 0 f.ring[0] = 0
} }
// ValidateCounter checks if the counter should be accepted. // ValidateCounter checks if the counter should be accepted.
// Overlimit counters (>= limit) are always rejected. // Overlimit counters (>= limit) are always rejected.
func (f *ReplayFilter) ValidateCounter(counter uint64, limit uint64) bool { func (f *Filter) ValidateCounter(counter uint64, limit uint64) bool {
if counter >= limit { if counter >= limit {
return false return false
} }

View file

@ -14,10 +14,10 @@ import (
* *
*/ */
const RejectAfterMessages = (1 << 64) - (1 << 4) - 1 const RejectAfterMessages = 1<<64 - 1<<13 - 1
func TestReplay(t *testing.T) { func TestReplay(t *testing.T) {
var filter ReplayFilter var filter Filter
const T_LIM = windowSize + 1 const T_LIM = windowSize + 1
@ -29,7 +29,7 @@ func TestReplay(t *testing.T) {
} }
} }
filter.Init() filter.Reset()
T(0, true) /* 1 */ T(0, true) /* 1 */
T(1, true) /* 2 */ T(1, true) /* 2 */
@ -67,7 +67,7 @@ func TestReplay(t *testing.T) {
T(0, false) /* 34 */ T(0, false) /* 34 */
t.Log("Bulk test 1") t.Log("Bulk test 1")
filter.Init() filter.Reset()
testNumber = 0 testNumber = 0
for i := uint64(1); i <= windowSize; i++ { for i := uint64(1); i <= windowSize; i++ {
T(i, true) T(i, true)
@ -76,7 +76,7 @@ func TestReplay(t *testing.T) {
T(0, false) T(0, false)
t.Log("Bulk test 2") t.Log("Bulk test 2")
filter.Init() filter.Reset()
testNumber = 0 testNumber = 0
for i := uint64(2); i <= windowSize+1; i++ { for i := uint64(2); i <= windowSize+1; i++ {
T(i, true) T(i, true)
@ -85,14 +85,14 @@ func TestReplay(t *testing.T) {
T(0, false) T(0, false)
t.Log("Bulk test 3") t.Log("Bulk test 3")
filter.Init() filter.Reset()
testNumber = 0 testNumber = 0
for i := uint64(windowSize + 1); i > 0; i-- { for i := uint64(windowSize + 1); i > 0; i-- {
T(i, true) T(i, true)
} }
t.Log("Bulk test 4") t.Log("Bulk test 4")
filter.Init() filter.Reset()
testNumber = 0 testNumber = 0
for i := uint64(windowSize + 2); i > 1; i-- { for i := uint64(windowSize + 2); i > 1; i-- {
T(i, true) T(i, true)
@ -100,7 +100,7 @@ func TestReplay(t *testing.T) {
T(0, false) T(0, false)
t.Log("Bulk test 5") t.Log("Bulk test 5")
filter.Init() filter.Reset()
testNumber = 0 testNumber = 0
for i := uint64(windowSize); i > 0; i-- { for i := uint64(windowSize); i > 0; i-- {
T(i, true) T(i, true)
@ -109,7 +109,7 @@ func TestReplay(t *testing.T) {
T(0, false) T(0, false)
t.Log("Bulk test 6") t.Log("Bulk test 6")
filter.Init() filter.Reset()
testNumber = 0 testNumber = 0
for i := uint64(windowSize); i > 0; i-- { for i := uint64(windowSize); i > 0; i-- {
T(i, true) T(i, true)