forked from vuvuzela/vuvuzela
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vuvuzela.go
92 lines (75 loc) · 1.96 KB
/
vuvuzela.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package vuvuzela
import (
"bytes"
"encoding/binary"
"unsafe"
"golang.org/x/crypto/nacl/box"
"github.com/davidlazar/vuvuzela/onionbox"
)
type DeadDrop [16]byte
const (
SizeEncryptedMessage = SizeMessage + box.Overhead
SizeConvoExchange = int(unsafe.Sizeof(ConvoExchange{}))
SizeEncryptedIntro = int(unsafe.Sizeof(Introduction{})) + onionbox.Overhead
SizeDialExchange = int(unsafe.Sizeof(DialExchange{}))
)
type ConvoExchange struct {
DeadDrop DeadDrop
EncryptedMessage [SizeEncryptedMessage]byte
}
func (e *ConvoExchange) Marshal() []byte {
buf := new(bytes.Buffer)
if err := binary.Write(buf, binary.BigEndian, e); err != nil {
panic(err)
}
return buf.Bytes()
}
func (e *ConvoExchange) Unmarshal(data []byte) error {
buf := bytes.NewReader(data)
return binary.Read(buf, binary.BigEndian, e)
}
type Introduction struct {
Rendezvous uint32
LongTermKey BoxKey
}
func (i *Introduction) Marshal() []byte {
buf := new(bytes.Buffer)
if err := binary.Write(buf, binary.BigEndian, i); err != nil {
panic(err)
}
return buf.Bytes()
}
func (i *Introduction) Unmarshal(data []byte) error {
buf := bytes.NewReader(data)
return binary.Read(buf, binary.BigEndian, i)
}
type DialExchange struct {
Bucket uint32
EncryptedIntro [SizeEncryptedIntro]byte
}
func (e *DialExchange) Marshal() []byte {
buf := new(bytes.Buffer)
if err := binary.Write(buf, binary.BigEndian, e); err != nil {
panic(err)
}
return buf.Bytes()
}
func (e *DialExchange) Unmarshal(data []byte) error {
buf := bytes.NewReader(data)
return binary.Read(buf, binary.BigEndian, e)
}
func ForwardNonce(round uint32) *[24]byte {
var nonce [24]byte
binary.BigEndian.PutUint32(nonce[0:4], round)
nonce[4] = 0
return &nonce
}
func BackwardNonce(round uint32) *[24]byte {
var nonce [24]byte
binary.BigEndian.PutUint32(nonce[0:4], round)
nonce[4] = 1
return &nonce
}
func KeyDialBucket(key *BoxKey, buckets uint32) uint32 {
return binary.BigEndian.Uint32(key[28:32]) % buckets
}