summaryrefslogtreecommitdiffhomepage
path: root/src/noise_protocol.go
blob: e7c87745154561f9428ba952a036c0d71c22450e (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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main

import (
	"errors"
	"golang.org/x/crypto/blake2s"
	"golang.org/x/crypto/chacha20poly1305"
	"golang.org/x/crypto/poly1305"
	"sync"
)

const (
	HandshakeInitialCreated = iota
	HandshakeInitialConsumed
	HandshakeResponseCreated
)

const (
	NoiseConstruction = "Noise_IKpsk2_25519_ChaChaPoly_BLAKE2s"
	WGIdentifier      = "WireGuard v1 zx2c4 Jason@zx2c4.com"
	WGLabelMAC1       = "mac1----"
	WGLabelCookie     = "cookie--"
)

const (
	MessageInitalType         = 1
	MessageResponseType       = 2
	MessageCookieResponseType = 3
	MessageTransportType      = 4
)

type MessageInital struct {
	Type      uint32
	Sender    uint32
	Ephemeral NoisePublicKey
	Static    [NoisePublicKeySize + poly1305.TagSize]byte
	Timestamp [TAI64NSize + poly1305.TagSize]byte
	Mac1      [blake2s.Size128]byte
	Mac2      [blake2s.Size128]byte
}

type MessageResponse struct {
	Type      uint32
	Sender    uint32
	Reciever  uint32
	Ephemeral NoisePublicKey
	Empty     [poly1305.TagSize]byte
	Mac1      [blake2s.Size128]byte
	Mac2      [blake2s.Size128]byte
}

type MessageTransport struct {
	Type     uint32
	Reciever uint32
	Counter  uint64
	Content  []byte
}

type Handshake struct {
	lock         sync.Mutex
	state        int
	chainKey     [blake2s.Size]byte // chain key
	hash         [blake2s.Size]byte // hash value
	staticStatic NoisePublicKey     // precomputed DH(S_i, S_r)
	ephemeral    NoisePrivateKey    // ephemeral secret key
	remoteIndex  uint32             // index for sending
	device       *Device
	peer         *Peer
}

var (
	ZeroNonce      [chacha20poly1305.NonceSize]byte
	InitalChainKey [blake2s.Size]byte
	InitalHash     [blake2s.Size]byte
)

func init() {
	InitalChainKey = blake2s.Sum256([]byte(NoiseConstruction))
	InitalHash = blake2s.Sum256(append(InitalChainKey[:], []byte(WGIdentifier)...))
}

func (h *Handshake) Precompute() {
	h.staticStatic = h.device.privateKey.sharedSecret(h.peer.publicKey)
}

func (h *Handshake) ConsumeMessageResponse(msg *MessageResponse) {

}

func (h *Handshake) addHash(data []byte) {
	h.hash = addToHash(h.hash, data)
}

func (h *Handshake) addChain(data []byte) {
	h.chainKey = addToChainKey(h.chainKey, data)
}

func (h *Handshake) CreateMessageInital() (*MessageInital, error) {
	h.lock.Lock()
	defer h.lock.Unlock()

	// reset handshake

	var err error
	h.ephemeral, err = newPrivateKey()
	if err != nil {
		return nil, err
	}
	h.chainKey = InitalChainKey
	h.hash = addToHash(InitalHash, h.device.publicKey[:])

	// create ephemeral key

	var msg MessageInital
	msg.Type = MessageInitalType
	msg.Sender = h.device.NewID(h)
	msg.Ephemeral = h.ephemeral.publicKey()
	h.chainKey = addToChainKey(h.chainKey, msg.Ephemeral[:])
	h.hash = addToHash(h.hash, msg.Ephemeral[:])

	// encrypt long-term "identity key"

	func() {
		var key [chacha20poly1305.KeySize]byte
		ss := h.ephemeral.sharedSecret(h.peer.publicKey)
		h.chainKey, key = KDF2(h.chainKey[:], ss[:])
		aead, _ := chacha20poly1305.New(key[:])
		aead.Seal(msg.Static[:0], ZeroNonce[:], h.device.publicKey[:], nil)
	}()
	h.addHash(msg.Static[:])

	// encrypt timestamp

	timestamp := Timestamp()
	func() {
		var key [chacha20poly1305.KeySize]byte
		h.chainKey, key = KDF2(h.chainKey[:], h.staticStatic[:])
		aead, _ := chacha20poly1305.New(key[:])
		aead.Seal(msg.Timestamp[:0], ZeroNonce[:], timestamp[:], nil)
	}()
	h.addHash(msg.Timestamp[:])
	h.state = HandshakeInitialCreated
	return &msg, nil
}

func (h *Handshake) ConsumeMessageInitial(msg *MessageInital) error {
	if msg.Type != MessageInitalType {
		panic(errors.New("bug: invalid inital message type"))
	}

	hash := addToHash(InitalHash, h.device.publicKey[:])
	chainKey := addToChainKey(InitalChainKey, msg.Ephemeral[:])
	hash = addToHash(hash, msg.Ephemeral[:])

	//

	ephemeral, err := newPrivateKey()
	if err != nil {
		return err
	}

	// update handshake state

	h.lock.Lock()
	defer h.lock.Unlock()

	h.hash = hash
	h.chainKey = chainKey
	h.remoteIndex = msg.Sender
	h.ephemeral = ephemeral
	h.state = HandshakeInitialConsumed

	return nil

}

func (h *Handshake) CreateMessageResponse() []byte {

	return nil
}