summaryrefslogtreecommitdiffhomepage
path: root/replay.go
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2018-05-23 02:32:02 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2018-05-23 03:58:27 +0200
commit5a2228a5c910ada948677f1dd3fcc59f74e5cb20 (patch)
treebafb31cbcc18221e2299f8fef21d9c2f4471f706 /replay.go
parent0a63188afab1dd49380f916963307f9b2efdcac1 (diff)
Move replay into subpackage
Diffstat (limited to 'replay.go')
-rw-r--r--replay.go79
1 files changed, 0 insertions, 79 deletions
diff --git a/replay.go b/replay.go
deleted file mode 100644
index 2d9c6e1..0000000
--- a/replay.go
+++ /dev/null
@@ -1,79 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0
- *
- * Copyright (C) 2017-2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
- * Copyright (C) 2017-2018 Mathias N. Hall-Andersen <mathias@hall-andersen.dk>.
- */
-
-package main
-
-/* Copyright (C) 2015-2017 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved. */
-
-/* Implementation of RFC6479
- * https://tools.ietf.org/html/rfc6479
- *
- * The implementation is not safe for concurrent use!
- */
-
-const (
- // See: https://golang.org/src/math/big/arith.go
- _Wordm = ^uintptr(0)
- _WordLogSize = _Wordm>>8&1 + _Wordm>>16&1 + _Wordm>>32&1
- _WordSize = 1 << _WordLogSize
-)
-
-const (
- CounterRedundantBitsLog = _WordLogSize + 3
- CounterRedundantBits = _WordSize * 8
- CounterBitsTotal = 2048
- CounterWindowSize = uint64(CounterBitsTotal - CounterRedundantBits)
-)
-
-const (
- BacktrackWords = CounterBitsTotal / _WordSize
-)
-
-type ReplayFilter struct {
- counter uint64
- backtrack [BacktrackWords]uintptr
-}
-
-func (filter *ReplayFilter) Init() {
- filter.counter = 0
- filter.backtrack[0] = 0
-}
-
-func (filter *ReplayFilter) ValidateCounter(counter uint64) bool {
- if counter >= RejectAfterMessages {
- return false
- }
-
- indexWord := counter >> CounterRedundantBitsLog
-
- if counter > filter.counter {
-
- // move window forward
-
- current := filter.counter >> CounterRedundantBitsLog
- diff := minUint64(indexWord-current, BacktrackWords)
- for i := uint64(1); i <= diff; i++ {
- filter.backtrack[(current+i)%BacktrackWords] = 0
- }
- filter.counter = counter
-
- } else if filter.counter-counter > CounterWindowSize {
-
- // behind current window
-
- return false
- }
-
- indexWord %= BacktrackWords
- indexBit := counter & uint64(CounterRedundantBits-1)
-
- // check and set bit
-
- oldValue := filter.backtrack[indexWord]
- newValue := oldValue | (1 << indexBit)
- filter.backtrack[indexWord] = newValue
- return oldValue != newValue
-}