summaryrefslogtreecommitdiffhomepage
path: root/src/keypair.go
blob: 3caa0c833262dabd599a08dd0b1dbfe0b05c4781 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package main

import (
	"crypto/cipher"
	"sync"
	"time"
)

type KeyPair struct {
	receive     cipher.AEAD
	send        cipher.AEAD
	sendNonce   uint64
	isInitiator bool
	created     time.Time
	localIndex  uint32
	remoteIndex 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
}