From 2c143dce0ff55feb35c7f6b9199479db88909903 Mon Sep 17 00:00:00 2001 From: Riobard Zhan Date: Thu, 10 Sep 2020 02:06:44 +0800 Subject: [PATCH] replay: minor API changes to more idiomatic Go Signed-off-by: Riobard Zhan Signed-off-by: Jason A. Donenfeld --- device/keypair.go | 2 +- device/noise-protocol.go | 2 +- replay/replay.go | 12 ++++++------ replay/replay_test.go | 18 +++++++++--------- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/device/keypair.go b/device/keypair.go index 2f2f222..254e696 100644 --- a/device/keypair.go +++ b/device/keypair.go @@ -26,7 +26,7 @@ type Keypair struct { sendNonce uint64 send cipher.AEAD receive cipher.AEAD - replayFilter replay.ReplayFilter + replayFilter replay.Filter isInitiator bool created time.Time localIndex uint32 diff --git a/device/noise-protocol.go b/device/noise-protocol.go index be92b4b..1dc854f 100644 --- a/device/noise-protocol.go +++ b/device/noise-protocol.go @@ -567,7 +567,7 @@ func (peer *Peer) BeginSymmetricSession() error { keypair.created = time.Now() keypair.sendNonce = 0 - keypair.replayFilter.Init() + keypair.replayFilter.Reset() keypair.isInitiator = isInitiator keypair.localIndex = peer.handshake.localIndex keypair.remoteIndex = peer.handshake.remoteIndex diff --git a/replay/replay.go b/replay/replay.go index 8685712..5b2de41 100644 --- a/replay/replay.go +++ b/replay/replay.go @@ -17,24 +17,24 @@ const ( 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. -// 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. -type ReplayFilter struct { +type Filter struct { last uint64 ring [ringBlocks]block } -// Init resets the filter to empty state. -func (f *ReplayFilter) Init() { +// Reset resets the filter to empty state. +func (f *Filter) Reset() { f.last = 0 f.ring[0] = 0 } // ValidateCounter checks if the counter should be accepted. // 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 { return false } diff --git a/replay/replay_test.go b/replay/replay_test.go index 5af66ff..fcffd20 100644 --- a/replay/replay_test.go +++ b/replay/replay_test.go @@ -14,10 +14,10 @@ import ( * */ -const RejectAfterMessages = (1 << 64) - (1 << 4) - 1 +const RejectAfterMessages = 1<<64 - 1<<13 - 1 func TestReplay(t *testing.T) { - var filter ReplayFilter + var filter Filter const T_LIM = windowSize + 1 @@ -29,7 +29,7 @@ func TestReplay(t *testing.T) { } } - filter.Init() + filter.Reset() T(0, true) /* 1 */ T(1, true) /* 2 */ @@ -67,7 +67,7 @@ func TestReplay(t *testing.T) { T(0, false) /* 34 */ t.Log("Bulk test 1") - filter.Init() + filter.Reset() testNumber = 0 for i := uint64(1); i <= windowSize; i++ { T(i, true) @@ -76,7 +76,7 @@ func TestReplay(t *testing.T) { T(0, false) t.Log("Bulk test 2") - filter.Init() + filter.Reset() testNumber = 0 for i := uint64(2); i <= windowSize+1; i++ { T(i, true) @@ -85,14 +85,14 @@ func TestReplay(t *testing.T) { T(0, false) t.Log("Bulk test 3") - filter.Init() + filter.Reset() testNumber = 0 for i := uint64(windowSize + 1); i > 0; i-- { T(i, true) } t.Log("Bulk test 4") - filter.Init() + filter.Reset() testNumber = 0 for i := uint64(windowSize + 2); i > 1; i-- { T(i, true) @@ -100,7 +100,7 @@ func TestReplay(t *testing.T) { T(0, false) t.Log("Bulk test 5") - filter.Init() + filter.Reset() testNumber = 0 for i := uint64(windowSize); i > 0; i-- { T(i, true) @@ -109,7 +109,7 @@ func TestReplay(t *testing.T) { T(0, false) t.Log("Bulk test 6") - filter.Init() + filter.Reset() testNumber = 0 for i := uint64(windowSize); i > 0; i-- { T(i, true)