Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace mix of atomics and rwmutex with mutex around key material #58

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions device/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,11 +392,10 @@ func (device *Device) SendKeepalivesToPeersWithCurrentKeypair() {

device.peers.RLock()
for _, peer := range device.peers.keyMap {
peer.keypairs.RLock()
sendKeepalive := peer.keypairs.current != nil && !peer.keypairs.current.created.Add(RejectAfterTime).Before(time.Now())
peer.keypairs.RUnlock()
if sendKeepalive {
peer.SendKeepalive()
if current := peer.keypairs.Current(); current != nil {
if current.created.Add(RejectAfterTime).Before(time.Now()) {
peer.SendKeepalive()
}
}
}
device.peers.RUnlock()
Expand Down
8 changes: 4 additions & 4 deletions device/keypair.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ type Keypair struct {
}

type Keypairs struct {
sync.RWMutex
sync.Mutex
current *Keypair
previous *Keypair
next atomic.Pointer[Keypair]
next *Keypair
}

func (kp *Keypairs) Current() *Keypair {
kp.RLock()
defer kp.RUnlock()
kp.Lock()
defer kp.Unlock()
return kp.current
}

Expand Down
15 changes: 6 additions & 9 deletions device/noise-protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,12 +581,12 @@ func (peer *Peer) BeginSymmetricSession() error {
defer keypairs.Unlock()

previous := keypairs.previous
next := keypairs.next.Load()
next := keypairs.next
current := keypairs.current

if isInitiator {
if next != nil {
keypairs.next.Store(nil)
keypairs.next = nil
keypairs.previous = next
device.DeleteKeypair(current)
} else {
Expand All @@ -595,7 +595,7 @@ func (peer *Peer) BeginSymmetricSession() error {
device.DeleteKeypair(previous)
keypairs.current = keypair
} else {
keypairs.next.Store(keypair)
keypairs.next = keypair
device.DeleteKeypair(next)
keypairs.previous = nil
device.DeleteKeypair(previous)
Expand All @@ -607,18 +607,15 @@ func (peer *Peer) BeginSymmetricSession() error {
func (peer *Peer) ReceivedWithKeypair(receivedKeypair *Keypair) bool {
keypairs := &peer.keypairs

if keypairs.next.Load() != receivedKeypair {
return false
}
keypairs.Lock()
defer keypairs.Unlock()
if keypairs.next.Load() != receivedKeypair {
if keypairs.next != receivedKeypair {
return false
}
old := keypairs.previous
keypairs.previous = keypairs.current
peer.device.DeleteKeypair(old)
keypairs.current = keypairs.next.Load()
keypairs.next.Store(nil)
keypairs.current = keypairs.next
keypairs.next = nil
return true
}
2 changes: 1 addition & 1 deletion device/noise_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func TestNoiseHandshake(t *testing.T) {
t.Fatal("failed to derive keypair for peer 2", err)
}

key1 := peer1.keypairs.next.Load()
key1 := peer1.keypairs.next
key2 := peer2.keypairs.current

// encrypting / decryption test
Expand Down
8 changes: 4 additions & 4 deletions device/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,10 @@ func (peer *Peer) ZeroAndFlushAll() {
keypairs.Lock()
device.DeleteKeypair(keypairs.previous)
device.DeleteKeypair(keypairs.current)
device.DeleteKeypair(keypairs.next.Load())
device.DeleteKeypair(keypairs.next)
keypairs.previous = nil
keypairs.current = nil
keypairs.next.Store(nil)
keypairs.next = nil
keypairs.Unlock()

// clear handshake state
Expand All @@ -232,8 +232,8 @@ func (peer *Peer) ExpireCurrentKeypairs() {
if keypairs.current != nil {
keypairs.current.sendNonce.Store(RejectAfterMessages)
}
if next := keypairs.next.Load(); next != nil {
next.sendNonce.Store(RejectAfterMessages)
if keypairs.next != nil {
keypairs.next.sendNonce.Store(RejectAfterMessages)
}
keypairs.Unlock()
}
Expand Down