Moved tai64n into sub-package

This commit is contained in:
Mathias Hall-Andersen 2018-02-11 19:25:33 +01:00
parent 743364f647
commit 1cf23c0005
3 changed files with 47 additions and 48 deletions

View file

@ -1,4 +1,4 @@
package main package tai64n
import ( import (
"bytes" "bytes"
@ -6,23 +6,21 @@ import (
"time" "time"
) )
const ( const TimestampSize = 12
TAI64NBase = uint64(4611686018427387914) const base = uint64(4611686018427387914)
TAI64NSize = 12
)
type TAI64N [TAI64NSize]byte type Timestamp [TimestampSize]byte
func Timestamp() TAI64N { func Now() Timestamp {
var tai64n TAI64N var tai64n Timestamp
now := time.Now() now := time.Now()
secs := TAI64NBase + uint64(now.Unix()) secs := base + uint64(now.Unix())
nano := uint32(now.UnixNano()) nano := uint32(now.UnixNano())
binary.BigEndian.PutUint64(tai64n[:], secs) binary.BigEndian.PutUint64(tai64n[:], secs)
binary.BigEndian.PutUint32(tai64n[8:], nano) binary.BigEndian.PutUint32(tai64n[8:], nano)
return tai64n return tai64n
} }
func (t1 *TAI64N) After(t2 TAI64N) bool { func (t1 Timestamp) After(t2 Timestamp) bool {
return bytes.Compare(t1[:], t2[:]) > 0 return bytes.Compare(t1[:], t2[:]) > 0
} }

View file

@ -2,6 +2,7 @@ package main
import ( import (
"errors" "errors"
"git.zx2c4.com/wireguard-go/internal/tai64n"
"golang.org/x/crypto/blake2s" "golang.org/x/crypto/blake2s"
"golang.org/x/crypto/chacha20poly1305" "golang.org/x/crypto/chacha20poly1305"
"golang.org/x/crypto/poly1305" "golang.org/x/crypto/poly1305"
@ -58,7 +59,7 @@ type MessageInitiation struct {
Sender uint32 Sender uint32
Ephemeral NoisePublicKey Ephemeral NoisePublicKey
Static [NoisePublicKeySize + poly1305.TagSize]byte Static [NoisePublicKeySize + poly1305.TagSize]byte
Timestamp [TAI64NSize + poly1305.TagSize]byte Timestamp [tai64n.TimestampSize + poly1305.TagSize]byte
MAC1 [blake2s.Size128]byte MAC1 [blake2s.Size128]byte
MAC2 [blake2s.Size128]byte MAC2 [blake2s.Size128]byte
} }
@ -99,7 +100,7 @@ type Handshake struct {
remoteStatic NoisePublicKey // long term key remoteStatic NoisePublicKey // long term key
remoteEphemeral NoisePublicKey // ephemeral public key remoteEphemeral NoisePublicKey // ephemeral public key
precomputedStaticStatic [NoisePublicKeySize]byte // precomputed shared secret precomputedStaticStatic [NoisePublicKeySize]byte // precomputed shared secret
lastTimestamp TAI64N lastTimestamp tai64n.Timestamp
lastInitiationConsumption time.Time lastInitiationConsumption time.Time
} }
@ -206,7 +207,7 @@ func (device *Device) CreateMessageInitiation(peer *Peer) (*MessageInitiation, e
// encrypt timestamp // encrypt timestamp
timestamp := Timestamp() timestamp := tai64n.Now()
func() { func() {
var key [chacha20poly1305.KeySize]byte var key [chacha20poly1305.KeySize]byte
KDF2( KDF2(
@ -271,7 +272,7 @@ func (device *Device) ConsumeMessageInitiation(msg *MessageInitiation) *Peer {
// verify identity // verify identity
var timestamp TAI64N var timestamp tai64n.Timestamp
var key [chacha20poly1305.KeySize]byte var key [chacha20poly1305.KeySize]byte
handshake.mutex.RLock() handshake.mutex.RLock()

View file

@ -24,40 +24,40 @@ func TestReplay(t *testing.T) {
filter.Init() filter.Init()
/* 1 */ T(0, true) T(0, true) /* 1 */
/* 2 */ T(1, true) T(1, true) /* 2 */
/* 3 */ T(1, false) T(1, false) /* 3 */
/* 4 */ T(9, true) T(9, true) /* 4 */
/* 5 */ T(8, true) T(8, true) /* 5 */
/* 6 */ T(7, true) T(7, true) /* 6 */
/* 7 */ T(7, false) T(7, false) /* 7 */
/* 8 */ T(T_LIM, true) T(T_LIM, true) /* 8 */
/* 9 */ T(T_LIM-1, true) T(T_LIM-1, true) /* 9 */
/* 10 */ T(T_LIM-1, false) T(T_LIM-1, false) /* 10 */
/* 11 */ T(T_LIM-2, true) T(T_LIM-2, true) /* 11 */
/* 12 */ T(2, true) T(2, true) /* 12 */
/* 13 */ T(2, false) T(2, false) /* 13 */
/* 14 */ T(T_LIM+16, true) T(T_LIM+16, true) /* 14 */
/* 15 */ T(3, false) T(3, false) /* 15 */
/* 16 */ T(T_LIM+16, false) T(T_LIM+16, false) /* 16 */
/* 17 */ T(T_LIM*4, true) T(T_LIM*4, true) /* 17 */
/* 18 */ T(T_LIM*4-(T_LIM-1), true) T(T_LIM*4-(T_LIM-1), true) /* 18 */
/* 19 */ T(10, false) T(10, false) /* 19 */
/* 20 */ T(T_LIM*4-T_LIM, false) T(T_LIM*4-T_LIM, false) /* 20 */
/* 21 */ T(T_LIM*4-(T_LIM+1), false) T(T_LIM*4-(T_LIM+1), false) /* 21 */
/* 22 */ T(T_LIM*4-(T_LIM-2), true) T(T_LIM*4-(T_LIM-2), true) /* 22 */
/* 23 */ T(T_LIM*4+1-T_LIM, false) T(T_LIM*4+1-T_LIM, false) /* 23 */
/* 24 */ T(0, false) T(0, false) /* 24 */
/* 25 */ T(RejectAfterMessages, false) T(RejectAfterMessages, false) /* 25 */
/* 26 */ T(RejectAfterMessages-1, true) T(RejectAfterMessages-1, true) /* 26 */
/* 27 */ T(RejectAfterMessages, false) T(RejectAfterMessages, false) /* 27 */
/* 28 */ T(RejectAfterMessages-1, false) T(RejectAfterMessages-1, false) /* 28 */
/* 29 */ T(RejectAfterMessages-2, true) T(RejectAfterMessages-2, true) /* 29 */
/* 30 */ T(RejectAfterMessages+1, false) T(RejectAfterMessages+1, false) /* 30 */
/* 31 */ T(RejectAfterMessages+2, false) T(RejectAfterMessages+2, false) /* 31 */
/* 32 */ T(RejectAfterMessages-2, false) T(RejectAfterMessages-2, false) /* 32 */
/* 33 */ T(RejectAfterMessages-3, true) T(RejectAfterMessages-3, true) /* 33 */
/* 34 */ T(0, false) T(0, false) /* 34 */
t.Log("Bulk test 1") t.Log("Bulk test 1")
filter.Init() filter.Init()