Started migration to sub-packages
This commit is contained in:
parent
51a6001bb9
commit
b461343171
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"crypto/hmac"
|
"crypto/hmac"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
|
"git.zx2c4.com/wireguard-go/internal/xchacha20poly1305"
|
||||||
"golang.org/x/crypto/blake2s"
|
"golang.org/x/crypto/blake2s"
|
||||||
"golang.org/x/crypto/chacha20poly1305"
|
"golang.org/x/crypto/chacha20poly1305"
|
||||||
"sync"
|
"sync"
|
||||||
|
@ -154,7 +155,7 @@ func (st *CookieChecker) CreateReply(
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
XChaCha20Poly1305Encrypt(
|
xchacha20poly1305.Encrypt(
|
||||||
reply.Cookie[:0],
|
reply.Cookie[:0],
|
||||||
&reply.Nonce,
|
&reply.Nonce,
|
||||||
cookie[:],
|
cookie[:],
|
||||||
|
@ -198,7 +199,7 @@ func (st *CookieGenerator) ConsumeReply(msg *MessageCookieReply) bool {
|
||||||
|
|
||||||
var cookie [blake2s.Size128]byte
|
var cookie [blake2s.Size128]byte
|
||||||
|
|
||||||
_, err := XChaCha20Poly1305Decrypt(
|
_, err := xchacha20poly1305.Decrypt(
|
||||||
cookie[:0],
|
cookie[:0],
|
||||||
&msg.Nonce,
|
&msg.Nonce,
|
||||||
msg.Cookie[:],
|
msg.Cookie[:],
|
||||||
|
|
36
internal/events/event.go
Normal file
36
internal/events/event.go
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
package events
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Event interface {
|
||||||
|
Contains(int) bool
|
||||||
|
Processed()
|
||||||
|
WaitForProcessed()
|
||||||
|
}
|
||||||
|
|
||||||
|
type EventStruct struct {
|
||||||
|
code int
|
||||||
|
lock sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
func (event EventStruct) Contains(code int) bool {
|
||||||
|
return event.code&code != 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (event *EventStruct) WaitForProcessed() {
|
||||||
|
event.lock.Lock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (event *EventStruct) Processed() {
|
||||||
|
event.lock.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewEvent(code int) Event {
|
||||||
|
event := &EventStruct{
|
||||||
|
code: code,
|
||||||
|
}
|
||||||
|
event.lock.Lock()
|
||||||
|
return event
|
||||||
|
}
|
|
@ -2,14 +2,14 @@
|
||||||
// Use of this source code is governed by a license that can be
|
// Use of this source code is governed by a license that can be
|
||||||
// found in the LICENSE file.
|
// found in the LICENSE file.
|
||||||
|
|
||||||
package main
|
package xchacha20poly1305
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"golang.org/x/crypto/chacha20poly1305"
|
"golang.org/x/crypto/chacha20poly1305"
|
||||||
)
|
)
|
||||||
|
|
||||||
func HChaCha20(out *[32]byte, nonce []byte, key *[32]byte) {
|
func hChaCha20(out *[32]byte, nonce []byte, key *[32]byte) {
|
||||||
|
|
||||||
v00 := uint32(0x61707865)
|
v00 := uint32(0x61707865)
|
||||||
v01 := uint32(0x3320646e)
|
v01 := uint32(0x3320646e)
|
||||||
|
@ -138,7 +138,7 @@ func HChaCha20(out *[32]byte, nonce []byte, key *[32]byte) {
|
||||||
binary.LittleEndian.PutUint32(out[28:], v15)
|
binary.LittleEndian.PutUint32(out[28:], v15)
|
||||||
}
|
}
|
||||||
|
|
||||||
func XChaCha20Poly1305Encrypt(
|
func Encrypt(
|
||||||
dst []byte,
|
dst []byte,
|
||||||
nonceFull *[24]byte,
|
nonceFull *[24]byte,
|
||||||
plaintext []byte,
|
plaintext []byte,
|
||||||
|
@ -147,13 +147,13 @@ func XChaCha20Poly1305Encrypt(
|
||||||
) []byte {
|
) []byte {
|
||||||
var nonce [chacha20poly1305.NonceSize]byte
|
var nonce [chacha20poly1305.NonceSize]byte
|
||||||
var derivedKey [chacha20poly1305.KeySize]byte
|
var derivedKey [chacha20poly1305.KeySize]byte
|
||||||
HChaCha20(&derivedKey, nonceFull[:16], key)
|
hChaCha20(&derivedKey, nonceFull[:16], key)
|
||||||
aead, _ := chacha20poly1305.New(derivedKey[:])
|
aead, _ := chacha20poly1305.New(derivedKey[:])
|
||||||
copy(nonce[4:], nonceFull[16:])
|
copy(nonce[4:], nonceFull[16:])
|
||||||
return aead.Seal(dst, nonce[:], plaintext, additionalData)
|
return aead.Seal(dst, nonce[:], plaintext, additionalData)
|
||||||
}
|
}
|
||||||
|
|
||||||
func XChaCha20Poly1305Decrypt(
|
func Decrypt(
|
||||||
dst []byte,
|
dst []byte,
|
||||||
nonceFull *[24]byte,
|
nonceFull *[24]byte,
|
||||||
plaintext []byte,
|
plaintext []byte,
|
||||||
|
@ -162,7 +162,7 @@ func XChaCha20Poly1305Decrypt(
|
||||||
) ([]byte, error) {
|
) ([]byte, error) {
|
||||||
var nonce [chacha20poly1305.NonceSize]byte
|
var nonce [chacha20poly1305.NonceSize]byte
|
||||||
var derivedKey [chacha20poly1305.KeySize]byte
|
var derivedKey [chacha20poly1305.KeySize]byte
|
||||||
HChaCha20(&derivedKey, nonceFull[:16], key)
|
hChaCha20(&derivedKey, nonceFull[:16], key)
|
||||||
aead, _ := chacha20poly1305.New(derivedKey[:])
|
aead, _ := chacha20poly1305.New(derivedKey[:])
|
||||||
copy(nonce[4:], nonceFull[16:])
|
copy(nonce[4:], nonceFull[16:])
|
||||||
return aead.Open(dst, nonce[:], plaintext, additionalData)
|
return aead.Open(dst, nonce[:], plaintext, additionalData)
|
|
@ -1,4 +1,4 @@
|
||||||
package main
|
package xchacha20poly1305
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
@ -60,7 +60,7 @@ func TestXChaCha20(t *testing.T) {
|
||||||
|
|
||||||
// test encryption
|
// test encryption
|
||||||
|
|
||||||
ct := XChaCha20Poly1305Encrypt(
|
ct := Encrypt(
|
||||||
nil,
|
nil,
|
||||||
&nonceArray,
|
&nonceArray,
|
||||||
pt,
|
pt,
|
||||||
|
@ -74,7 +74,7 @@ func TestXChaCha20(t *testing.T) {
|
||||||
|
|
||||||
// test decryption
|
// test decryption
|
||||||
|
|
||||||
ptp, err := XChaCha20Poly1305Decrypt(
|
ptp, err := Decrypt(
|
||||||
nil,
|
nil,
|
||||||
&nonceArray,
|
&nonceArray,
|
||||||
ct,
|
ct,
|
14
tun.go
14
tun.go
|
@ -1,14 +1,13 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"git.zx2c4.com/wireguard-go/internal/events"
|
||||||
"os"
|
"os"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
)
|
)
|
||||||
|
|
||||||
const DefaultMTU = 1420
|
const DefaultMTU = 1420
|
||||||
|
|
||||||
type TUNEvent int
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
TUNEventUp = 1 << iota
|
TUNEventUp = 1 << iota
|
||||||
TUNEventDown
|
TUNEventDown
|
||||||
|
@ -21,7 +20,7 @@ type TUNDevice interface {
|
||||||
Write([]byte, int) (int, error) // writes a packet to the device (without any additional headers)
|
Write([]byte, int) (int, error) // writes a packet to the device (without any additional headers)
|
||||||
MTU() (int, error) // returns the MTU of the device
|
MTU() (int, error) // returns the MTU of the device
|
||||||
Name() string // returns the current name
|
Name() string // returns the current name
|
||||||
Events() chan TUNEvent // returns a constant channel of events related to the device
|
Events() chan events.Event // returns a constant channel of events related to the device
|
||||||
Close() error // stops the device and closes the event channel
|
Close() error // stops the device and closes the event channel
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,7 +29,8 @@ func (device *Device) RoutineTUNEventReader() {
|
||||||
logError := device.log.Error
|
logError := device.log.Error
|
||||||
|
|
||||||
for event := range device.tun.device.Events() {
|
for event := range device.tun.device.Events() {
|
||||||
if event&TUNEventMTUUpdate != 0 {
|
|
||||||
|
if event.Contains(TUNEventMTUUpdate) {
|
||||||
mtu, err := device.tun.device.MTU()
|
mtu, err := device.tun.device.MTU()
|
||||||
old := atomic.LoadInt32(&device.tun.mtu)
|
old := atomic.LoadInt32(&device.tun.mtu)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -45,14 +45,16 @@ func (device *Device) RoutineTUNEventReader() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if event&TUNEventUp != 0 && !device.isUp.Get() {
|
if event.Contains(TUNEventUp) && !device.isUp.Get() {
|
||||||
logInfo.Println("Interface set up")
|
logInfo.Println("Interface set up")
|
||||||
device.Up()
|
device.Up()
|
||||||
}
|
}
|
||||||
|
|
||||||
if event&TUNEventDown != 0 && device.isUp.Get() {
|
if event.Contains(TUNEventDown) && device.isUp.Get() {
|
||||||
logInfo.Println("Interface set down")
|
logInfo.Println("Interface set down")
|
||||||
device.Down()
|
device.Down()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
event.Processed()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
25
tun_linux.go
25
tun_linux.go
|
@ -7,6 +7,7 @@ import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"git.zx2c4.com/wireguard-go/internal/events"
|
||||||
"golang.org/x/net/ipv6"
|
"golang.org/x/net/ipv6"
|
||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
"net"
|
"net"
|
||||||
|
@ -52,10 +53,10 @@ const (
|
||||||
|
|
||||||
type NativeTun struct {
|
type NativeTun struct {
|
||||||
fd *os.File
|
fd *os.File
|
||||||
index int32 // if index
|
index int32 // if index
|
||||||
name string // name of interface
|
name string // name of interface
|
||||||
errors chan error // async error handling
|
errors chan error // async error handling
|
||||||
events chan TUNEvent // device related events
|
events chan events.Event // device related events
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *NativeTun) File() *os.File {
|
func (tun *NativeTun) File() *os.File {
|
||||||
|
@ -71,9 +72,9 @@ func (tun *NativeTun) RoutineHackListener() {
|
||||||
_, err := unix.Write(fd, nil)
|
_, err := unix.Write(fd, nil)
|
||||||
switch err {
|
switch err {
|
||||||
case unix.EINVAL:
|
case unix.EINVAL:
|
||||||
tun.events <- TUNEventUp
|
tun.events <- events.NewEvent(TUNEventUp)
|
||||||
case unix.EIO:
|
case unix.EIO:
|
||||||
tun.events <- TUNEventDown
|
tun.events <- events.NewEvent(TUNEventDown)
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
time.Sleep(time.Second / 10)
|
time.Sleep(time.Second / 10)
|
||||||
|
@ -118,14 +119,14 @@ func (tun *NativeTun) RoutineNetlinkListener() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if info.Flags&unix.IFF_RUNNING != 0 {
|
if info.Flags&unix.IFF_RUNNING != 0 {
|
||||||
tun.events <- TUNEventUp
|
tun.events <- events.NewEvent(TUNEventUp)
|
||||||
}
|
}
|
||||||
|
|
||||||
if info.Flags&unix.IFF_RUNNING == 0 {
|
if info.Flags&unix.IFF_RUNNING == 0 {
|
||||||
tun.events <- TUNEventDown
|
tun.events <- events.NewEvent(TUNEventDown)
|
||||||
}
|
}
|
||||||
|
|
||||||
tun.events <- TUNEventMTUUpdate
|
tun.events <- events.NewEvent(TUNEventMTUUpdate)
|
||||||
|
|
||||||
default:
|
default:
|
||||||
remain = remain[hdr.Len:]
|
remain = remain[hdr.Len:]
|
||||||
|
@ -288,7 +289,7 @@ func (tun *NativeTun) Read(buff []byte, offset int) (int, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *NativeTun) Events() chan TUNEvent {
|
func (tun *NativeTun) Events() chan events.Event {
|
||||||
return tun.events
|
return tun.events
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -300,7 +301,7 @@ func CreateTUNFromFile(name string, fd *os.File) (TUNDevice, error) {
|
||||||
device := &NativeTun{
|
device := &NativeTun{
|
||||||
fd: fd,
|
fd: fd,
|
||||||
name: name,
|
name: name,
|
||||||
events: make(chan TUNEvent, 5),
|
events: make(chan events.Event, 5),
|
||||||
errors: make(chan error, 5),
|
errors: make(chan error, 5),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -357,7 +358,7 @@ func CreateTUN(name string) (TUNDevice, error) {
|
||||||
device := &NativeTun{
|
device := &NativeTun{
|
||||||
fd: fd,
|
fd: fd,
|
||||||
name: newName,
|
name: newName,
|
||||||
events: make(chan TUNEvent, 5),
|
events: make(chan events.Event, 5),
|
||||||
errors: make(chan error, 5),
|
errors: make(chan error, 5),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue