import (
"crypto/cipher"
"sync"
+ "sync/atomic"
"time"
+ "unsafe"
"golang.zx2c4.com/wireguard/replay"
)
sync.RWMutex
current *Keypair
previous *Keypair
- next *Keypair
+ next unsafe.Pointer // *Keypair, access via LoadNext/StoreNext
+}
+
+func (kp *Keypairs) StoreNext(next *Keypair) {
+ atomic.StorePointer(&kp.next, (unsafe.Pointer)(next))
+}
+
+func (kp *Keypairs) LoadNext() *Keypair {
+ return (*Keypair)(atomic.LoadPointer(&kp.next))
}
func (kp *Keypairs) Current() *Keypair {
"golang.org/x/crypto/blake2s"
"golang.org/x/crypto/chacha20poly1305"
"golang.org/x/crypto/poly1305"
+
"golang.zx2c4.com/wireguard/tai64n"
)
defer keypairs.Unlock()
previous := keypairs.previous
- next := keypairs.next
+ next := keypairs.LoadNext()
current := keypairs.current
if isInitiator {
if next != nil {
- keypairs.next = nil
+ keypairs.StoreNext(nil)
keypairs.previous = next
device.DeleteKeypair(current)
} else {
device.DeleteKeypair(previous)
keypairs.current = keypair
} else {
- keypairs.next = keypair
+ keypairs.StoreNext(keypair)
device.DeleteKeypair(next)
keypairs.previous = nil
device.DeleteKeypair(previous)
func (peer *Peer) ReceivedWithKeypair(receivedKeypair *Keypair) bool {
keypairs := &peer.keypairs
+
+ if keypairs.LoadNext() != receivedKeypair {
+ return false
+ }
keypairs.Lock()
defer keypairs.Unlock()
- if keypairs.next != receivedKeypair {
+ if keypairs.LoadNext() != receivedKeypair {
return false
}
old := keypairs.previous
keypairs.previous = keypairs.current
peer.device.DeleteKeypair(old)
- keypairs.current = keypairs.next
- keypairs.next = nil
+ keypairs.current = keypairs.LoadNext()
+ keypairs.StoreNext(nil)
return true
}
t.Fatal("failed to derive keypair for peer 2", err)
}
- key1 := peer1.keypairs.next
+ key1 := peer1.keypairs.LoadNext()
key2 := peer2.keypairs.current
// encrypting / decryption test
keypairs.Lock()
device.DeleteKeypair(keypairs.previous)
device.DeleteKeypair(keypairs.current)
- device.DeleteKeypair(keypairs.next)
+ device.DeleteKeypair(keypairs.LoadNext())
keypairs.previous = nil
keypairs.current = nil
- keypairs.next = nil
+ keypairs.StoreNext(nil)
keypairs.Unlock()
// clear handshake state
keypairs.current.sendNonce = RejectAfterMessages
}
if keypairs.next != nil {
- keypairs.next.sendNonce = RejectAfterMessages
+ keypairs.LoadNext().sendNonce = RejectAfterMessages
}
keypairs.Unlock()
}