From ea4ea6f9334b8979bec6a881d7f94d7fa94e9b9c Mon Sep 17 00:00:00 2001 From: Mathias Hall-Andersen Date: Mon, 12 Feb 2018 22:29:11 +0100 Subject: [PATCH] Revert "Don't use modules" This reverts commit bffe99aeadae09abd02f2bd3184925af6b680535. --- cookie.go | 5 +++-- device.go | 3 ++- ratelimiter.go => internal/ratelimiter/ratelimiter.go | 2 +- .../ratelimiter/ratelimiter_test.go | 2 +- tai64n.go => internal/tai64n/tai64n.go | 4 ++-- tai64n_test.go => internal/tai64n/tai64n_test.go | 6 +++--- xchacha20.go => internal/xchacha20poly1305/xchacha20.go | 6 +++--- .../xchacha20poly1305/xchacha20_test.go | 6 +++--- noise-protocol.go | 9 +++++---- 9 files changed, 23 insertions(+), 20 deletions(-) rename ratelimiter.go => internal/ratelimiter/ratelimiter.go (99%) rename ratelimiter_test.go => internal/ratelimiter/ratelimiter_test.go (99%) rename tai64n.go => internal/tai64n/tai64n.go (91%) rename tai64n_test.go => internal/tai64n/tai64n_test.go (84%) rename xchacha20.go => internal/xchacha20poly1305/xchacha20.go (98%) rename xchacha20_test.go => internal/xchacha20poly1305/xchacha20_test.go (96%) diff --git a/cookie.go b/cookie.go index 9572901..72455a4 100644 --- a/cookie.go +++ b/cookie.go @@ -3,6 +3,7 @@ package main import ( "crypto/hmac" "crypto/rand" + "git.zx2c4.com/wireguard-go/internal/xchacha20poly1305" "golang.org/x/crypto/blake2s" "golang.org/x/crypto/chacha20poly1305" "sync" @@ -157,7 +158,7 @@ func (st *CookieChecker) CreateReply( return nil, err } - XChaCha20Poly1305Encrypt( + xchacha20poly1305.Encrypt( reply.Cookie[:0], &reply.Nonce, cookie[:], @@ -201,7 +202,7 @@ func (st *CookieGenerator) ConsumeReply(msg *MessageCookieReply) bool { var cookie [blake2s.Size128]byte - _, err := XChaCha20Poly1305Decrypt( + _, err := xchacha20poly1305.Decrypt( cookie[:0], &msg.Nonce, msg.Cookie[:], diff --git a/device.go b/device.go index 6324948..9f93f21 100644 --- a/device.go +++ b/device.go @@ -1,6 +1,7 @@ package main import ( + "git.zx2c4.com/wireguard-go/internal/ratelimiter" "runtime" "sync" "sync/atomic" @@ -50,7 +51,7 @@ type Device struct { rate struct { underLoadUntil atomic.Value - limiter Ratelimiter + limiter ratelimiter.Ratelimiter } pool struct { diff --git a/ratelimiter.go b/internal/ratelimiter/ratelimiter.go similarity index 99% rename from ratelimiter.go rename to internal/ratelimiter/ratelimiter.go index 168a743..006900a 100644 --- a/ratelimiter.go +++ b/internal/ratelimiter/ratelimiter.go @@ -1,4 +1,4 @@ -package main +package ratelimiter /* Copyright (C) 2015-2017 Jason A. Donenfeld . All Rights Reserved. */ diff --git a/ratelimiter_test.go b/internal/ratelimiter/ratelimiter_test.go similarity index 99% rename from ratelimiter_test.go rename to internal/ratelimiter/ratelimiter_test.go index 9b0abfd..37339ee 100644 --- a/ratelimiter_test.go +++ b/internal/ratelimiter/ratelimiter_test.go @@ -1,4 +1,4 @@ -package main +package ratelimiter import ( "net" diff --git a/tai64n.go b/internal/tai64n/tai64n.go similarity index 91% rename from tai64n.go rename to internal/tai64n/tai64n.go index 8c5ebe0..da5257c 100644 --- a/tai64n.go +++ b/internal/tai64n/tai64n.go @@ -1,4 +1,4 @@ -package main +package tai64n import ( "bytes" @@ -11,7 +11,7 @@ const base = uint64(4611686018427387914) type Timestamp [TimestampSize]byte -func TimestampNow() Timestamp { +func Now() Timestamp { var tai64n Timestamp now := time.Now() secs := base + uint64(now.Unix()) diff --git a/tai64n_test.go b/internal/tai64n/tai64n_test.go similarity index 84% rename from tai64n_test.go rename to internal/tai64n/tai64n_test.go index a9e22b0..389b65c 100644 --- a/tai64n_test.go +++ b/internal/tai64n/tai64n_test.go @@ -1,4 +1,4 @@ -package main +package tai64n import ( "testing" @@ -9,10 +9,10 @@ import ( * as used by WireGuard. */ func TestMonotonic(t *testing.T) { - old := TimestampNow() + old := Now() for i := 0; i < 10000; i++ { time.Sleep(time.Nanosecond) - next := TimestampNow() + next := Now() if !next.After(old) { t.Error("TAI64N, not monotonically increasing on nano-second scale") } diff --git a/xchacha20.go b/internal/xchacha20poly1305/xchacha20.go similarity index 98% rename from xchacha20.go rename to internal/xchacha20poly1305/xchacha20.go index 8e7d99f..a6e59f0 100644 --- a/xchacha20.go +++ b/internal/xchacha20poly1305/xchacha20.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a license that can be // found in the LICENSE file. -package main +package xchacha20poly1305 import ( "encoding/binary" @@ -138,7 +138,7 @@ func hChaCha20(out *[32]byte, nonce []byte, key *[32]byte) { binary.LittleEndian.PutUint32(out[28:], v15) } -func XChaCha20Poly1305Encrypt( +func Encrypt( dst []byte, nonceFull *[24]byte, plaintext []byte, @@ -153,7 +153,7 @@ func XChaCha20Poly1305Encrypt( return aead.Seal(dst, nonce[:], plaintext, additionalData) } -func XChaCha20Poly1305Decrypt( +func Decrypt( dst []byte, nonceFull *[24]byte, plaintext []byte, diff --git a/xchacha20_test.go b/internal/xchacha20poly1305/xchacha20_test.go similarity index 96% rename from xchacha20_test.go rename to internal/xchacha20poly1305/xchacha20_test.go index 0f41cf8..5d5b78f 100644 --- a/xchacha20_test.go +++ b/internal/xchacha20poly1305/xchacha20_test.go @@ -1,4 +1,4 @@ -package main +package xchacha20poly1305 import ( "encoding/hex" @@ -60,7 +60,7 @@ func TestXChaCha20(t *testing.T) { // test encryption - ct := XChaCha20Poly1305Encrypt( + ct := Encrypt( nil, &nonceArray, pt, @@ -74,7 +74,7 @@ func TestXChaCha20(t *testing.T) { // test decryption - ptp, err := XChaCha20Poly1305Decrypt( + ptp, err := Decrypt( nil, &nonceArray, ct, diff --git a/noise-protocol.go b/noise-protocol.go index 31ae55b..df08a0a 100644 --- a/noise-protocol.go +++ b/noise-protocol.go @@ -2,6 +2,7 @@ package main import ( "errors" + "git.zx2c4.com/wireguard-go/internal/tai64n" "golang.org/x/crypto/blake2s" "golang.org/x/crypto/chacha20poly1305" "golang.org/x/crypto/poly1305" @@ -58,7 +59,7 @@ type MessageInitiation struct { Sender uint32 Ephemeral NoisePublicKey Static [NoisePublicKeySize + poly1305.TagSize]byte - Timestamp [TimestampSize + poly1305.TagSize]byte + Timestamp [tai64n.TimestampSize + poly1305.TagSize]byte MAC1 [blake2s.Size128]byte MAC2 [blake2s.Size128]byte } @@ -99,7 +100,7 @@ type Handshake struct { remoteStatic NoisePublicKey // long term key remoteEphemeral NoisePublicKey // ephemeral public key precomputedStaticStatic [NoisePublicKeySize]byte // precomputed shared secret - lastTimestamp Timestamp + lastTimestamp tai64n.Timestamp lastInitiationConsumption time.Time } @@ -206,7 +207,7 @@ func (device *Device) CreateMessageInitiation(peer *Peer) (*MessageInitiation, e // encrypt timestamp - timestamp := TimestampNow() + timestamp := tai64n.Now() func() { var key [chacha20poly1305.KeySize]byte KDF2( @@ -271,7 +272,7 @@ func (device *Device) ConsumeMessageInitiation(msg *MessageInitiation) *Peer { // verify identity - var timestamp Timestamp + var timestamp tai64n.Timestamp var key [chacha20poly1305.KeySize]byte handshake.mutex.RLock()