-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathdisco.go
More file actions
240 lines (207 loc) · 6.21 KB
/
disco.go
File metadata and controls
240 lines (207 loc) · 6.21 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
package key
import (
"bytes"
"crypto/subtle"
"fmt"
"go4.org/mem"
"golang.org/x/crypto/curve25519"
"golang.org/x/crypto/nacl/box"
"tailscale.com/types/structs"
)
const (
// discoPublicHexPrefix is the prefix used to identify a
// hex-encoded disco public key.
//
// This prefix is used in the control protocol, so cannot be
// changed.
discoPublicHexPrefix = "discokey:"
// DiscoPublicRawLen is the length in bytes of a DiscoPublic, when
// serialized with AppendTo, Raw32 or WriteRawWithoutAllocating.
DiscoPublicRawLen = 32
)
// DiscoPrivate is a disco key, used for peer-to-peer path discovery.
type DiscoPrivate struct {
_ structs.Incomparable // because == isn't constant-time
k [32]byte
}
// NewDisco creates and returns a new disco private key.
func NewDisco() DiscoPrivate {
var ret DiscoPrivate
rand(ret.k[:])
// Key used for nacl seal/open, so needs to be clamped.
clamp25519Private(ret.k[:])
return ret
}
// DiscoPrivateFromRaw32 parses a 32-byte raw value as a DiscoPrivate.
func DiscoPrivateFromRaw32(raw mem.RO) DiscoPrivate {
if raw.Len() != 32 {
panic("input has wrong size")
}
var ret DiscoPrivate
raw.Copy(ret.k[:])
return ret
}
// IsZero reports whether k is the zero value.
func (k DiscoPrivate) IsZero() bool {
return k.Equal(DiscoPrivate{})
}
// Equal reports whether k and other are the same key.
func (k DiscoPrivate) Equal(other DiscoPrivate) bool {
return subtle.ConstantTimeCompare(k.k[:], other.k[:]) == 1
}
// Public returns the DiscoPublic for k.
// Panics if DiscoPrivate is zero.
func (k DiscoPrivate) Public() DiscoPublic {
if k.IsZero() {
panic("can't take the public key of a zero DiscoPrivate")
}
var ret DiscoPublic
curve25519.ScalarBaseMult(&ret.k, &k.k)
return ret
}
// Shared returns the DiscoShared for communication between k and p.
func (k DiscoPrivate) Shared(p DiscoPublic) DiscoShared {
if k.IsZero() || p.IsZero() {
panic("can't compute shared secret with zero keys")
}
var ret DiscoShared
box.Precompute(&ret.k, &p.k, &k.k)
return ret
}
// SortedPairOfDiscoPublic is a lexicographically sorted container of two
// [DiscoPublic] keys.
type SortedPairOfDiscoPublic struct {
k [2]DiscoPublic
}
// Get returns the underlying keys.
func (s SortedPairOfDiscoPublic) Get() [2]DiscoPublic {
return s.k
}
// NewSortedPairOfDiscoPublic returns a SortedPairOfDiscoPublic from a and b.
func NewSortedPairOfDiscoPublic(a, b DiscoPublic) SortedPairOfDiscoPublic {
s := SortedPairOfDiscoPublic{}
if a.Compare(b) < 0 {
s.k[0] = a
s.k[1] = b
} else {
s.k[0] = b
s.k[1] = a
}
return s
}
func (s SortedPairOfDiscoPublic) String() string {
return fmt.Sprintf("%s <=> %s", s.k[0].ShortString(), s.k[1].ShortString())
}
// Equal returns true if s and b are equal, otherwise it returns false.
func (s SortedPairOfDiscoPublic) Equal(b SortedPairOfDiscoPublic) bool {
for i := range s.k {
if s.k[i].Compare(b.k[i]) != 0 {
return false
}
}
return true
}
// DiscoPublic is the public portion of a DiscoPrivate.
type DiscoPublic struct {
k [32]byte
}
// DiscoPublicFromRaw32 parses a 32-byte raw value as a DiscoPublic.
//
// This should be used only when deserializing a DiscoPublic from a
// binary protocol.
func DiscoPublicFromRaw32(raw mem.RO) DiscoPublic {
if raw.Len() != 32 {
panic("input has wrong size")
}
var ret DiscoPublic
raw.Copy(ret.k[:])
return ret
}
// IsZero reports whether k is the zero value.
func (k DiscoPublic) IsZero() bool {
return k == DiscoPublic{}
}
// Raw32 returns k encoded as 32 raw bytes.
//
// Deprecated: only needed for a temporary compat shim in tailcfg, do
// not add more uses.
func (k DiscoPublic) Raw32() [32]byte {
return k.k
}
// ShortString returns the Tailscale conventional debug representation
// of a disco key.
func (k DiscoPublic) ShortString() string {
if k.IsZero() {
return ""
}
return fmt.Sprintf("d:%x", k.k[:8])
}
// AppendTo appends k, serialized as a 32-byte binary value, to
// buf. Returns the new slice.
func (k DiscoPublic) AppendTo(buf []byte) []byte {
return append(buf, k.k[:]...)
}
// String returns the output of MarshalText as a string.
func (k DiscoPublic) String() string {
bs, err := k.MarshalText()
if err != nil {
panic(err)
}
return string(bs)
}
// Compare returns an integer comparing DiscoPublic k and l lexicographically.
// The result will be 0 if k == other, -1 if k < other, and +1 if k > other.
// This is useful for situations requiring only one node in a pair to perform
// some operation, e.g. probing UDP path lifetime.
func (k DiscoPublic) Compare(other DiscoPublic) int {
return bytes.Compare(k.k[:], other.k[:])
}
// AppendText implements encoding.TextAppender.
func (k DiscoPublic) AppendText(b []byte) ([]byte, error) {
return appendHexKey(b, discoPublicHexPrefix, k.k[:]), nil
}
// MarshalText implements encoding.TextMarshaler.
func (k DiscoPublic) MarshalText() ([]byte, error) {
return k.AppendText(nil)
}
// MarshalText implements encoding.TextUnmarshaler.
func (k *DiscoPublic) UnmarshalText(b []byte) error {
return parseHex(k.k[:], mem.B(b), mem.S(discoPublicHexPrefix))
}
type DiscoShared struct {
_ structs.Incomparable // because == isn't constant-time
k [32]byte
}
// Equal reports whether k and other are the same key.
func (k DiscoShared) Equal(other DiscoShared) bool {
return subtle.ConstantTimeCompare(k.k[:], other.k[:]) == 1
}
func (k DiscoShared) IsZero() bool {
return k.Equal(DiscoShared{})
}
// Seal wraps cleartext into a NaCl box (see
// golang.org/x/crypto/nacl), using k as the shared secret and a
// random nonce.
func (k DiscoShared) Seal(cleartext []byte) (ciphertext []byte) {
if k.IsZero() {
panic("can't seal with zero key")
}
var nonce [24]byte
rand(nonce[:])
return box.SealAfterPrecomputation(nonce[:], cleartext, &nonce, &k.k)
}
// Open opens the NaCl box ciphertext, which must be a value created
// by Seal, and returns the inner cleartext if ciphertext is a valid
// box using shared secret k.
func (k DiscoShared) Open(ciphertext []byte) (cleartext []byte, ok bool) {
if k.IsZero() {
panic("can't open with zero key")
}
if len(ciphertext) < 24 {
return nil, false
}
nonce := (*[24]byte)(ciphertext)
return box.OpenAfterPrecomputation(nil, ciphertext[24:], nonce, &k.k)
}