-
Notifications
You must be signed in to change notification settings - Fork 16
/
sphinx.go
441 lines (381 loc) · 13.2 KB
/
sphinx.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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// sphinx.go - Sphinx Packet Format.
// Copyright (C) 2017 Yawning Angel.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// Package sphinx implements the Katzenpost parameterized Sphinx Packet Format.
package sphinx
import (
"crypto/subtle"
"errors"
"fmt"
"io"
"github.com/katzenpost/hpqc/kem"
kemschemes "github.com/katzenpost/hpqc/kem/schemes"
"github.com/katzenpost/hpqc/nike"
"github.com/katzenpost/hpqc/nike/schemes"
"github.com/katzenpost/hpqc/rand"
"github.com/katzenpost/katzenpost/core/sphinx/commands"
"github.com/katzenpost/katzenpost/core/sphinx/constants"
"github.com/katzenpost/katzenpost/core/sphinx/geo"
"github.com/katzenpost/katzenpost/core/sphinx/internal/crypto"
"github.com/katzenpost/katzenpost/core/utils"
)
var (
v0AD = [2]byte{0x00, 0x00}
errTruncatedPayload = errors.New("sphinx: truncated payload")
errInvalidTag = errors.New("sphinx: payload auth failed")
)
// Sphinx is a modular implementation of the Sphinx cryptographic packet
// format that has a pluggable NIKE, non-interactive key exchange.
type Sphinx struct {
nike nike.Scheme
kem kem.Scheme
geometry *geo.Geometry
}
// FromGeometry returns Sphinx type given a valid Geometry.
func FromGeometry(geometry *geo.Geometry) (*Sphinx, error) {
if geometry.NIKEName != "" {
mynike := schemes.ByName(geometry.NIKEName)
if mynike == nil {
return nil, fmt.Errorf("cannot lookup NIKE by name: `%s`", geometry.NIKEName)
}
return &Sphinx{
nike: mynike,
geometry: geometry,
}, nil
}
mykem := kemschemes.ByName(geometry.KEMName)
if mykem == nil {
return nil, fmt.Errorf("cannot lookup KEM by name: `%s`", geometry.KEMName)
}
return &Sphinx{
kem: mykem,
geometry: geometry,
}, nil
}
func NewNIKESphinx(mynike nike.Scheme, geo *geo.Geometry) *Sphinx {
return &Sphinx{
nike: mynike,
geometry: geo,
}
}
// NewSphinx creates a new instance of Sphinx.
func NewSphinx(geo *geo.Geometry) *Sphinx {
s, err := FromGeometry(geo)
if err != nil {
panic(err)
}
return s
}
// Geometry returns the Sphinx packet geometry.
func (s *Sphinx) Geometry() *geo.Geometry {
return s.geometry
}
// PathHop describes a hop that a Sphinx Packet will traverse, along with
// all of the per-hop Commands (excluding NextNodeHop).
type PathHop struct {
ID [constants.NodeIDLength]byte
NIKEPublicKey nike.PublicKey
KEMPublicKey kem.PublicKey
Commands []commands.RoutingCommand
}
type sprpKey struct {
key [crypto.SPRPKeyLength]byte
iv [crypto.SPRPIVLength]byte
}
func (k *sprpKey) Reset() {
utils.ExplicitBzero(k.key[:])
utils.ExplicitBzero(k.iv[:])
}
func (s *Sphinx) commandsToBytes(cmds []commands.RoutingCommand, isTerminal bool) ([]byte, error) {
b := make([]byte, 0, s.geometry.PerHopRoutingInfoLength)
for _, v := range cmds {
// NextNodeHop is generated by the header creation process.
if _, isNextNodeHop := v.(*commands.NextNodeHop); isNextNodeHop {
return nil, errors.New("sphinx: invalid commands, NextNodeHop")
}
b = v.ToBytes(b)
}
if len(b) > s.geometry.PerHopRoutingInfoLength {
return nil, errors.New("sphinx: invalid commands, oversized serialized block")
}
if !isTerminal && cap(b)-len(b) < s.geometry.NextNodeHopLength {
return nil, errors.New("sphinx: invalid commands, insufficient remaining capacity")
}
return b, nil
}
func (s *Sphinx) createHeader(r io.Reader, path []*PathHop) ([]byte, []*sprpKey, error) {
nrHops := len(path)
if nrHops > s.geometry.NrHops {
return nil, nil, errors.New("sphinx: invalid path")
}
// Derive the key material for each hop.
clientPublicKey, clientPrivateKey, err := s.nike.GenerateKeyPair()
if err != nil {
return nil, nil, err
}
defer clientPrivateKey.Reset()
defer clientPublicKey.Reset()
groupElements := make([]nike.PublicKey, s.geometry.NrHops)
keys := make([]*crypto.PacketKeys, s.geometry.NrHops)
sharedSecret := s.nike.DeriveSecret(clientPrivateKey, path[0].NIKEPublicKey)
defer utils.ExplicitBzero(sharedSecret)
keys[0] = crypto.KDF(sharedSecret, s.nike)
defer keys[0].Reset()
groupElements[0], err = s.nike.UnmarshalBinaryPublicKey(clientPublicKey.Bytes())
if err != nil {
panic(err)
}
for i := 1; i < nrHops; i++ {
sharedSecret = s.nike.DeriveSecret(clientPrivateKey, path[i].NIKEPublicKey)
for j := 0; j < i; j++ {
pubkey := s.nike.NewEmptyPublicKey()
err = pubkey.FromBytes(sharedSecret)
if err != nil {
panic(err)
}
blinded := s.nike.Blind(pubkey, keys[j].BlindingFactor)
sharedSecret = blinded.Bytes()
}
keys[i] = crypto.KDF(sharedSecret, s.nike)
defer keys[i].Reset()
err = clientPublicKey.Blind(keys[i-1].BlindingFactor)
if err != nil {
panic(err)
}
groupElements[i], err = s.nike.UnmarshalBinaryPublicKey(clientPublicKey.Bytes())
if err != nil {
panic(err)
}
}
// Derive the routing_information keystream and encrypted padding for each
// hop.
riKeyStream := make([][]byte, s.geometry.NrHops)
riPadding := make([][]byte, s.geometry.NrHops)
for i := 0; i < nrHops; i++ {
keyStream := make([]byte, s.geometry.RoutingInfoLength+s.geometry.PerHopRoutingInfoLength)
defer utils.ExplicitBzero(keyStream)
streamCipher := crypto.NewStream(&keys[i].HeaderEncryption, &keys[i].HeaderEncryptionIV)
streamCipher.KeyStream(keyStream)
streamCipher.Reset()
ksLen := len(keyStream) - (i+1)*s.geometry.PerHopRoutingInfoLength
riKeyStream[i] = keyStream[:ksLen]
riPadding[i] = keyStream[ksLen:]
if i > 0 {
prevPadLen := len(riPadding[i-1])
xorBytes(riPadding[i][:prevPadLen], riPadding[i][:prevPadLen], riPadding[i-1])
}
}
// Create the routing_information block.
var mac []byte
var routingInfo []byte
if skippedHops := s.geometry.NrHops - nrHops; skippedHops > 0 {
routingInfo = make([]byte, skippedHops*s.geometry.PerHopRoutingInfoLength)
_, err := io.ReadFull(rand.Reader, routingInfo)
if err != nil {
return nil, nil, err
}
}
zeroBytes := make([]byte, s.geometry.PerHopRoutingInfoLength)
for i := nrHops - 1; i >= 0; i-- {
isTerminal := i == nrHops-1
riFragment, err := s.commandsToBytes(path[i].Commands, isTerminal)
if err != nil {
return nil, nil, err
}
if !isTerminal {
nextCmd := &commands.NextNodeHop{}
copy(nextCmd.ID[:], path[i+1].ID[:])
copy(nextCmd.MAC[:], mac)
riFragment = nextCmd.ToBytes(riFragment)
}
if padLen := s.geometry.PerHopRoutingInfoLength - len(riFragment); padLen > 0 {
riFragment = append(riFragment, zeroBytes[:padLen]...)
}
routingInfo = append(riFragment, routingInfo...) // Prepend
xorBytes(routingInfo, routingInfo, riKeyStream[i])
m := crypto.NewMAC(&keys[i].HeaderMAC)
defer m.Reset()
m.Write(v0AD[:])
m.Write(groupElements[i].Bytes())
m.Write(routingInfo)
if i > 0 {
m.Write(riPadding[i-1])
}
mac = m.Sum(nil)
}
// Assemble the completed Sphinx Packet Header and Sphinx Packet Payload
// SPRP key vector.
hdr := make([]byte, 0, s.geometry.HeaderLength)
hdr = append(hdr, v0AD[:]...)
hdr = append(hdr, groupElements[0].Bytes()...)
hdr = append(hdr, routingInfo...)
hdr = append(hdr, mac...)
sprpKeys := make([]*sprpKey, 0, nrHops)
for i := 0; i < nrHops; i++ {
v := keys[i]
// The header encryption IV is reused for the SPRP because the keys
// *and* more importantly the primitives are different.
k := new(sprpKey)
copy(k.key[:], v.PayloadEncryption[:])
copy(k.iv[:], v.HeaderEncryptionIV[:])
sprpKeys = append(sprpKeys, k)
}
return hdr, sprpKeys, nil
}
func (s *Sphinx) newNikePacket(r io.Reader, path []*PathHop, payload []byte) ([]byte, error) {
if len(payload) != s.geometry.ForwardPayloadLength {
return nil, fmt.Errorf("invalid payload length: %d, expected %d", len(payload), s.geometry.ForwardPayloadLength)
}
hdr, sprpKeys, err := s.createHeader(r, path)
if err != nil {
return nil, err
}
for _, v := range sprpKeys {
defer v.Reset()
}
zeroBytes := make([]byte, s.geometry.PerHopRoutingInfoLength)
// Assemble the packet.
pkt := make([]byte, 0, len(hdr)+s.geometry.PayloadTagLength+len(payload))
pkt = append(pkt, hdr...)
pkt = append(pkt, zeroBytes[:s.geometry.PayloadTagLength]...)
pkt = append(pkt, payload...)
// Encrypt the payload.
b := pkt[len(hdr):]
for i := len(path) - 1; i >= 0; i-- {
k := sprpKeys[i]
b = crypto.SPRPEncrypt(&k.key, &k.iv, b)
}
copy(pkt[len(hdr):], b)
return pkt, nil
}
// Unwrap unwraps the provided Sphinx packet pkt in-place, using the provided
// NIKE private key, and returns the payload (if applicable), replay tag, and
// routing info command vector.
func (s *Sphinx) unwrapNike(privKey nike.PrivateKey, pkt []byte) ([]byte, []byte, []commands.RoutingCommand, error) {
var (
geOff = 2
riOff = geOff + s.nike.PublicKeySize()
macOff = riOff + s.geometry.RoutingInfoLength
payloadOff = macOff + crypto.MACLength
)
// Do some basic sanity checking, and validate the AD.
if len(pkt) < s.geometry.HeaderLength {
return nil, nil, nil, errors.New("sphinx: invalid packet, truncated")
}
if subtle.ConstantTimeCompare(v0AD[:], pkt[:2]) != 1 {
return nil, nil, nil, errors.New("sphinx: invalid packet, unknown version")
}
var sharedSecret []byte
defer utils.ExplicitBzero(sharedSecret)
// Calculate the hop's shared secret, and replay_tag.
groupElement, err := s.nike.UnmarshalBinaryPublicKey(pkt[geOff:riOff])
if err != nil {
return nil, nil, nil, fmt.Errorf("sphinx: failed to unmarshal group element: %s", err)
}
sharedSecret = s.nike.DeriveSecret(privKey, groupElement)
replayTag := crypto.Hash(groupElement.Bytes())
// Derive the various keys required for packet processing.
keys := crypto.KDF(sharedSecret, s.nike)
defer keys.Reset()
// Validate the Sphinx Packet Header.
m := crypto.NewMAC(&keys.HeaderMAC)
defer m.Reset()
m.Write(pkt[0:macOff])
mac := m.Sum(nil)
if subtle.ConstantTimeCompare(pkt[macOff:macOff+crypto.MACLength], mac) != 1 {
return nil, replayTag[:], nil, errors.New("sphinx: invalid packet, MAC mismatch")
}
// Append padding to preserve length invariance, decrypt the (padded)
// routing_info block, and extract the section for the current hop.
b := make([]byte, s.geometry.RoutingInfoLength+s.geometry.PerHopRoutingInfoLength)
copy(b[:s.geometry.RoutingInfoLength], pkt[riOff:riOff+s.geometry.RoutingInfoLength])
stream := crypto.NewStream(&keys.HeaderEncryption, &keys.HeaderEncryptionIV)
defer stream.Reset()
stream.XORKeyStream(b[:], b[:])
newRoutingInfo := b[s.geometry.PerHopRoutingInfoLength:]
cmdBuf := b[:s.geometry.PerHopRoutingInfoLength]
// Parse the per-hop routing commands.
var nextNode *commands.NextNodeHop
var surbReply *commands.SURBReply
cmds := make([]commands.RoutingCommand, 0, 2) // Usually 2, excluding null.
for {
cmd, rest, err := commands.FromBytes(cmdBuf, s.geometry)
if err != nil {
return nil, replayTag[:], nil, err
} else if cmd == nil { // Terminal null command.
if rest != nil {
// Bug, should NEVER happen.
return nil, replayTag[:], nil, errors.New("sphinx: BUG: null cmd had rest")
}
break
}
switch c := cmd.(type) {
case *commands.NextNodeHop:
if nextNode != nil {
return nil, replayTag[:], nil, errors.New("sphinx: invalid packet, > 1 next_node")
}
nextNode = c
case *commands.SURBReply:
if surbReply != nil {
return nil, replayTag[:], nil, errors.New("sphinx: invalid packet, > 1 surb_reply")
}
surbReply = c
default:
}
cmds = append(cmds, cmd)
cmdBuf = rest
}
// Decrypt the Sphinx Packet Payload.
payload := pkt[payloadOff:]
if len(payload) > 0 {
payload = crypto.SPRPDecrypt(&keys.PayloadEncryption, &keys.HeaderEncryptionIV, payload)
}
// Transform the packet for forwarding to the next mix, iff the
// routing commands vector included a NextNodeHopCommand.
if nextNode != nil {
err := groupElement.Blind(keys.BlindingFactor)
if err != nil {
panic(err)
}
copy(pkt[geOff:riOff], groupElement.Bytes()[:])
copy(pkt[riOff:macOff], newRoutingInfo)
copy(pkt[macOff:payloadOff], nextNode.MAC[:])
if len(payload) > 0 {
copy(pkt[payloadOff:], payload)
}
payload = nil
} else {
if len(payload) < s.geometry.PayloadTagLength {
return nil, replayTag[:], nil, errTruncatedPayload
}
// Validate the payload tag, iff this is not a SURB reply.
if surbReply == nil {
if !utils.CtIsZero(payload[:s.geometry.PayloadTagLength]) {
return nil, replayTag[:], nil, errInvalidTag
}
payload = payload[s.geometry.PayloadTagLength:]
}
}
return payload, replayTag[:], cmds, nil
}
func xorBytes(dst, a, b []byte) {
if len(a) != len(b) || len(a) != len(dst) {
panic(fmt.Sprintf("sphinx: BUG: xorBytes called with mismatched buffer sizes, got 'len(a)' %d and 'len(b)' %d", len(a), len(b)))
}
// TODO: If this shows up in the profiles, vectorize it.
for i, v := range a {
dst[i] = v ^ b[i]
}
}