import (
"crypto/hmac"
"crypto/rand"
+ "git.zx2c4.com/wireguard-go/internal/xchacha20poly1305"
"golang.org/x/crypto/blake2s"
"golang.org/x/crypto/chacha20poly1305"
"sync"
return nil, err
}
- XChaCha20Poly1305Encrypt(
+ xchacha20poly1305.Encrypt(
reply.Cookie[:0],
&reply.Nonce,
cookie[:],
var cookie [blake2s.Size128]byte
- _, err := XChaCha20Poly1305Decrypt(
+ _, err := xchacha20poly1305.Decrypt(
cookie[:0],
&msg.Nonce,
msg.Cookie[:],
--- /dev/null
+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
+}
// 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)
binary.LittleEndian.PutUint32(out[28:], v15)
}
-func XChaCha20Poly1305Encrypt(
+func Encrypt(
dst []byte,
nonceFull *[24]byte,
plaintext []byte,
) []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,
) ([]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)
-package main
+package xchacha20poly1305
import (
"encoding/hex"
// test encryption
- ct := XChaCha20Poly1305Encrypt(
+ ct := Encrypt(
nil,
&nonceArray,
pt,
// test decryption
- ptp, err := XChaCha20Poly1305Decrypt(
+ ptp, err := Decrypt(
nil,
&nonceArray,
ct,
package main
import (
+ "git.zx2c4.com/wireguard-go/internal/events"
"os"
"sync/atomic"
)
const DefaultMTU = 1420
-type TUNEvent int
-
const (
TUNEventUp = 1 << iota
TUNEventDown
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
}
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 {
}
}
- 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()
}
}
"encoding/binary"
"errors"
"fmt"
+ "git.zx2c4.com/wireguard-go/internal/events"
"golang.org/x/net/ipv6"
"golang.org/x/sys/unix"
"net"
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 {
_, 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)
}
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:]
}
}
-func (tun *NativeTun) Events() chan TUNEvent {
+func (tun *NativeTun) Events() chan events.Event {
return tun.events
}
device := &NativeTun{
fd: fd,
name: name,
- events: make(chan TUNEvent, 5),
+ events: make(chan events.Event, 5),
errors: make(chan error, 5),
}
device := &NativeTun{
fd: fd,
name: newName,
- events: make(chan TUNEvent, 5),
+ events: make(chan events.Event, 5),
errors: make(chan error, 5),
}