wireguard-go/src/keypair.go
2017-06-30 14:41:08 +02:00

44 lines
734 B
Go

package main
import (
"crypto/cipher"
"sync"
"time"
)
type KeyPair struct {
recv cipher.AEAD
recvNonce uint64
send cipher.AEAD
sendNonce uint64
isInitiator bool
created time.Time
id uint32
}
type KeyPairs struct {
mutex sync.RWMutex
current *KeyPair
previous *KeyPair
next *KeyPair // not yet "confirmed by transport"
}
/* Called during recieving to confirm the handshake
* was completed correctly
*/
func (kp *KeyPairs) Used(key *KeyPair) {
if key == kp.next {
kp.mutex.Lock()
kp.previous = kp.current
kp.current = key
kp.next = nil
kp.mutex.Unlock()
}
}
func (kp *KeyPairs) Current() *KeyPair {
kp.mutex.RLock()
defer kp.mutex.RUnlock()
return kp.current
}