Started migration to sub-packages
This commit is contained in:
parent
51a6001bb9
commit
b461343171
@ -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"
|
||||
@ -154,7 +155,7 @@ func (st *CookieChecker) CreateReply(
|
||||
return nil, err
|
||||
}
|
||||
|
||||
XChaCha20Poly1305Encrypt(
|
||||
xchacha20poly1305.Encrypt(
|
||||
reply.Cookie[:0],
|
||||
&reply.Nonce,
|
||||
cookie[:],
|
||||
@ -198,7 +199,7 @@ func (st *CookieGenerator) ConsumeReply(msg *MessageCookieReply) bool {
|
||||
|
||||
var cookie [blake2s.Size128]byte
|
||||
|
||||
_, err := XChaCha20Poly1305Decrypt(
|
||||
_, err := xchacha20poly1305.Decrypt(
|
||||
cookie[:0],
|
||||
&msg.Nonce,
|
||||
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
|
||||
// found in the LICENSE file.
|
||||
|
||||
package main
|
||||
package xchacha20poly1305
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"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)
|
||||
v01 := uint32(0x3320646e)
|
||||
@ -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,
|
||||
@ -147,13 +147,13 @@ func XChaCha20Poly1305Encrypt(
|
||||
) []byte {
|
||||
var nonce [chacha20poly1305.NonceSize]byte
|
||||
var derivedKey [chacha20poly1305.KeySize]byte
|
||||
HChaCha20(&derivedKey, nonceFull[:16], key)
|
||||
hChaCha20(&derivedKey, nonceFull[:16], key)
|
||||
aead, _ := chacha20poly1305.New(derivedKey[:])
|
||||
copy(nonce[4:], nonceFull[16:])
|
||||
return aead.Seal(dst, nonce[:], plaintext, additionalData)
|
||||
}
|
||||
|
||||
func XChaCha20Poly1305Decrypt(
|
||||
func Decrypt(
|
||||
dst []byte,
|
||||
nonceFull *[24]byte,
|
||||
plaintext []byte,
|
||||
@ -162,7 +162,7 @@ func XChaCha20Poly1305Decrypt(
|
||||
) ([]byte, error) {
|
||||
var nonce [chacha20poly1305.NonceSize]byte
|
||||
var derivedKey [chacha20poly1305.KeySize]byte
|
||||
HChaCha20(&derivedKey, nonceFull[:16], key)
|
||||
hChaCha20(&derivedKey, nonceFull[:16], key)
|
||||
aead, _ := chacha20poly1305.New(derivedKey[:])
|
||||
copy(nonce[4:], nonceFull[16:])
|
||||
return aead.Open(dst, nonce[:], plaintext, additionalData)
|
@ -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,
|
14
tun.go
14
tun.go
@ -1,14 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"git.zx2c4.com/wireguard-go/internal/events"
|
||||
"os"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
const DefaultMTU = 1420
|
||||
|
||||
type TUNEvent int
|
||||
|
||||
const (
|
||||
TUNEventUp = 1 << iota
|
||||
TUNEventDown
|
||||
@ -21,7 +20,7 @@ type TUNDevice interface {
|
||||
Write([]byte, int) (int, error) // writes a packet to the device (without any additional headers)
|
||||
MTU() (int, error) // returns the MTU of the device
|
||||
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
|
||||
}
|
||||
|
||||
@ -30,7 +29,8 @@ func (device *Device) RoutineTUNEventReader() {
|
||||
logError := device.log.Error
|
||||
|
||||
for event := range device.tun.device.Events() {
|
||||
if event&TUNEventMTUUpdate != 0 {
|
||||
|
||||
if event.Contains(TUNEventMTUUpdate) {
|
||||
mtu, err := device.tun.device.MTU()
|
||||
old := atomic.LoadInt32(&device.tun.mtu)
|
||||
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")
|
||||
device.Up()
|
||||
}
|
||||
|
||||
if event&TUNEventDown != 0 && device.isUp.Get() {
|
||||
if event.Contains(TUNEventDown) && device.isUp.Get() {
|
||||
logInfo.Println("Interface set down")
|
||||
device.Down()
|
||||
}
|
||||
|
||||
event.Processed()
|
||||
}
|
||||
}
|
||||
|
25
tun_linux.go
25
tun_linux.go
@ -7,6 +7,7 @@ import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.zx2c4.com/wireguard-go/internal/events"
|
||||
"golang.org/x/net/ipv6"
|
||||
"golang.org/x/sys/unix"
|
||||
"net"
|
||||
@ -52,10 +53,10 @@ const (
|
||||
|
||||
type NativeTun struct {
|
||||
fd *os.File
|
||||
index int32 // if index
|
||||
name string // name of interface
|
||||
errors chan error // async error handling
|
||||
events chan TUNEvent // device related events
|
||||
index int32 // if index
|
||||
name string // name of interface
|
||||
errors chan error // async error handling
|
||||
events chan events.Event // device related events
|
||||
}
|
||||
|
||||
func (tun *NativeTun) File() *os.File {
|
||||
@ -71,9 +72,9 @@ func (tun *NativeTun) RoutineHackListener() {
|
||||
_, err := unix.Write(fd, nil)
|
||||
switch err {
|
||||
case unix.EINVAL:
|
||||
tun.events <- TUNEventUp
|
||||
tun.events <- events.NewEvent(TUNEventUp)
|
||||
case unix.EIO:
|
||||
tun.events <- TUNEventDown
|
||||
tun.events <- events.NewEvent(TUNEventDown)
|
||||
default:
|
||||
}
|
||||
time.Sleep(time.Second / 10)
|
||||
@ -118,14 +119,14 @@ func (tun *NativeTun) RoutineNetlinkListener() {
|
||||
}
|
||||
|
||||
if info.Flags&unix.IFF_RUNNING != 0 {
|
||||
tun.events <- TUNEventUp
|
||||
tun.events <- events.NewEvent(TUNEventUp)
|
||||
}
|
||||
|
||||
if info.Flags&unix.IFF_RUNNING == 0 {
|
||||
tun.events <- TUNEventDown
|
||||
tun.events <- events.NewEvent(TUNEventDown)
|
||||
}
|
||||
|
||||
tun.events <- TUNEventMTUUpdate
|
||||
tun.events <- events.NewEvent(TUNEventMTUUpdate)
|
||||
|
||||
default:
|
||||
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
|
||||
}
|
||||
|
||||
@ -300,7 +301,7 @@ func CreateTUNFromFile(name string, fd *os.File) (TUNDevice, error) {
|
||||
device := &NativeTun{
|
||||
fd: fd,
|
||||
name: name,
|
||||
events: make(chan TUNEvent, 5),
|
||||
events: make(chan events.Event, 5),
|
||||
errors: make(chan error, 5),
|
||||
}
|
||||
|
||||
@ -357,7 +358,7 @@ func CreateTUN(name string) (TUNDevice, error) {
|
||||
device := &NativeTun{
|
||||
fd: fd,
|
||||
name: newName,
|
||||
events: make(chan TUNEvent, 5),
|
||||
events: make(chan events.Event, 5),
|
||||
errors: make(chan error, 5),
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user