forked from kryptco/kr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transport_mock_pair.go
68 lines (62 loc) · 1.46 KB
/
transport_mock_pair.go
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package kr
import (
"encoding/base64"
"sync"
)
type ImmediatePairTransport struct {
NoopTransport
sync.Mutex
Keys map[string][]byte
}
func (t *ImmediatePairTransport) Setup(ps *PairingSecret) (err error) {
return
}
func (t *ImmediatePairTransport) Read(notifier *Notifier, ps *PairingSecret) (ciphertexts [][]byte, err error) {
t.Lock()
defer t.Unlock()
if t.Keys == nil {
t.Keys = map[string][]byte{}
}
if _, ok := t.Keys[base64.StdEncoding.EncodeToString(ps.WorkstationPublicKey)]; !ok {
var key []byte
key, err = RandNBytes(32)
if err != nil {
return
}
t.Keys[base64.StdEncoding.EncodeToString(ps.WorkstationPublicKey)] = key
wrappedKey, wrapErr := WrapKey(t.Keys[base64.StdEncoding.EncodeToString(ps.WorkstationPublicKey)], ps.WorkstationPublicKey)
if wrapErr != nil {
err = wrapErr
return
}
ciphertexts = [][]byte{wrappedKey}
}
return
}
// store first key, but send multiple wrapped keys
type MultiPairTransport struct {
NoopTransport
sync.Mutex
paired bool
SymKey []byte
}
func (t *MultiPairTransport) Read(notifier *Notifier, ps *PairingSecret) (ciphertexts [][]byte, err error) {
t.Lock()
defer t.Unlock()
for _ = range []int{1, 2, 3} {
if !t.paired {
t.SymKey, err = RandNBytes(32)
if err != nil {
return
}
t.paired = true
}
wrappedKey, wrapErr := WrapKey(t.SymKey, ps.WorkstationPublicKey)
if wrapErr != nil {
err = wrapErr
return
}
ciphertexts = append(ciphertexts, wrappedKey)
}
return
}