Move replay into subpackage

This commit is contained in:
Jason A. Donenfeld 2018-05-23 02:32:02 +02:00
parent 0a63188afa
commit 5a2228a5c9
5 changed files with 17 additions and 16 deletions

View file

@ -7,6 +7,7 @@
package main package main
import ( import (
"./replay"
"crypto/cipher" "crypto/cipher"
"sync" "sync"
"time" "time"
@ -23,7 +24,7 @@ type Keypair struct {
sendNonce uint64 sendNonce uint64
send cipher.AEAD send cipher.AEAD
receive cipher.AEAD receive cipher.AEAD
replayFilter ReplayFilter replayFilter replay.ReplayFilter
isInitiator bool isInitiator bool
created time.Time created time.Time
localIndex uint32 localIndex uint32

View file

@ -47,10 +47,3 @@ func min(a, b uint) uint {
} }
return a return a
} }
func minUint64(a uint64, b uint64) uint64 {
if a > b {
return b
}
return a
}

View file

@ -544,7 +544,7 @@ func (peer *Peer) RoutineSequentialReceiver() {
// check for replay // check for replay
if !elem.keypair.replayFilter.ValidateCounter(elem.counter) { if !elem.keypair.replayFilter.ValidateCounter(elem.counter, RejectAfterMessages) {
continue continue
} }

View file

@ -4,9 +4,7 @@
* Copyright (C) 2017-2018 Mathias N. Hall-Andersen <mathias@hall-andersen.dk>. * Copyright (C) 2017-2018 Mathias N. Hall-Andersen <mathias@hall-andersen.dk>.
*/ */
package main package replay
/* Copyright (C) 2015-2017 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved. */
/* Implementation of RFC6479 /* Implementation of RFC6479
* https://tools.ietf.org/html/rfc6479 * https://tools.ietf.org/html/rfc6479
@ -32,6 +30,13 @@ const (
BacktrackWords = CounterBitsTotal / _WordSize BacktrackWords = CounterBitsTotal / _WordSize
) )
func minUint64(a uint64, b uint64) uint64 {
if a > b {
return b
}
return a
}
type ReplayFilter struct { type ReplayFilter struct {
counter uint64 counter uint64
backtrack [BacktrackWords]uintptr backtrack [BacktrackWords]uintptr
@ -42,8 +47,8 @@ func (filter *ReplayFilter) Init() {
filter.backtrack[0] = 0 filter.backtrack[0] = 0
} }
func (filter *ReplayFilter) ValidateCounter(counter uint64) bool { func (filter *ReplayFilter) ValidateCounter(counter uint64, limit uint64) bool {
if counter >= RejectAfterMessages { if counter >= limit {
return false return false
} }

View file

@ -4,7 +4,7 @@
* Copyright (C) 2017-2018 Mathias N. Hall-Andersen <mathias@hall-andersen.dk>. * Copyright (C) 2017-2018 Mathias N. Hall-Andersen <mathias@hall-andersen.dk>.
*/ */
package main package replay
import ( import (
"testing" "testing"
@ -15,6 +15,8 @@ import (
* *
*/ */
const RejectAfterMessages = (1 << 64) - (1 << 4) - 1
func TestReplay(t *testing.T) { func TestReplay(t *testing.T) {
var filter ReplayFilter var filter ReplayFilter
@ -23,7 +25,7 @@ func TestReplay(t *testing.T) {
testNumber := 0 testNumber := 0
T := func(n uint64, v bool) { T := func(n uint64, v bool) {
testNumber++ testNumber++
if filter.ValidateCounter(n) != v { if filter.ValidateCounter(n, RejectAfterMessages) != v {
t.Fatal("Test", testNumber, "failed", n, v) t.Fatal("Test", testNumber, "failed", n, v)
} }
} }