This repository has been archived by the owner on Jan 20, 2020. It is now read-only.
/
sphinx.go
477 lines (396 loc) · 15.8 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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
// Copyright 2018 The Nym Mixnet Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/* Package sphinx implements the library of a cryptographic packet format,
which can be used to secure the content as well as the metadata of the transported
messages.
*/
package sphinx
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"errors"
"fmt"
"strings"
"github.com/golang/protobuf/proto"
"github.com/nymtech/nym-mixnet/config"
"github.com/nymtech/nym-mixnet/flags"
"golang.org/x/crypto/curve25519"
)
const (
// K TODO: document padding-related Sphinx parameter
K = 16
headerLength = 192
)
// PackForwardMessage encapsulates the given message into the cryptographic Sphinx packet format.
// As arguments the function takes the path, consisting of the sequence of nodes the packet should traverse
// and the destination of the message, a set of delays and the information about the curve used to perform cryptographic
// operations.
// In order to encapsulate the message PackForwardMessage computes two parts of the packet - the header and
// the encrypted payload. If creating of any of the packet block failed, an error is returned. Otherwise,
// a Sphinx packet format is returned.
func PackForwardMessage(path config.E2EPath, delays []float64, message []byte) (SphinxPacket, error) {
nodes := []config.MixConfig{path.IngressProvider}
nodes = append(nodes, path.Mixes...)
nodes = append(nodes, path.EgressProvider)
dest := path.Recipient
headerInitials, header, err := createHeader(nodes, delays, dest)
if err != nil {
errMsg := fmt.Errorf("error in PackForwardMessage - createHeader failed: %v", err)
return SphinxPacket{}, errMsg
}
payload, err := encapsulateContent(headerInitials, message)
if err != nil {
errMsg := fmt.Errorf("error in PackForwardMessage - encapsulateContent failed: %v", err)
return SphinxPacket{}, errMsg
}
return SphinxPacket{Hdr: &header, Pld: payload}, nil
}
// createHeader builds the Sphinx packet header, consisting of three parts: the public element,
// the encapsulated routing information and the message authentication code.
// createHeader layer encapsulates the routing information for each given node. The routing information
// contains information where the packet should be forwarded next, how long it should be delayed by the node,
// and if relevant additional auxiliary information. The message authentication code allows to detect tagging attacks.
// createHeader computes the secret shared key between sender and the nodes and destination,
// which are used as keys for encryption.
// createHeader returns the header and a list of the initial elements, used for creating the header.
// If any operation was unsuccessful createHeader returns an error.
func createHeader(nodes []config.MixConfig,
delays []float64,
dest config.ClientConfig,
) ([]HeaderInitials, Header, error) {
x, err := RandomElement()
if err != nil {
errMsg := fmt.Errorf("error in createHeader - Random failed: %v", err)
return nil, Header{}, errMsg
}
headerInitials, err := getSharedSecrets(nodes, x)
if err != nil {
errMsg := fmt.Errorf("error in createHeader - getSharedSecrets failed: %v", err)
return nil, Header{}, errMsg
}
if len(headerInitials) != len(nodes) {
errMsg := fmt.Errorf("error in createHeader - wrong number of shared secrets failed: %v", err)
return nil, Header{}, errMsg
}
commands := make([]Commands, len(nodes))
for i := range nodes {
var c Commands
if i == len(nodes)-1 {
c = Commands{Delay: delays[i], Flag: flags.LastHopFlag.Bytes()}
} else {
c = Commands{Delay: delays[i], Flag: flags.RelayFlag.Bytes()}
}
commands[i] = c
}
header, err := encapsulateHeader(headerInitials, nodes, commands, dest)
if err != nil {
errMsg := fmt.Errorf("error in createHeader - encapsulateHeader failed: %v", err)
return nil, Header{}, errMsg
}
return headerInitials, header, nil
}
// encapsulateHeader layer encrypts the meta-data of the packet, containing information about the
// sequence of nodes the packet should traverse before reaching the destination, and message authentication codes,
// given the pre-computed shared keys which are used for encryption.
// encapsulateHeader returns the Header, or an error if any internal cryptographic of parsing operation failed.
func encapsulateHeader(headerInitials []HeaderInitials,
nodes []config.MixConfig,
commands []Commands,
destination config.ClientConfig,
) (Header, error) {
finalHop := RoutingInfo{NextHop: &Hop{Id: destination.Id,
Address: destination.Host + ":" + destination.Port,
PubKey: []byte{},
}, RoutingCommands: &commands[len(commands)-1],
NextHopMetaData: []byte{},
Mac: []byte{},
}
finalHopBytes, err := proto.Marshal(&finalHop)
if err != nil {
return Header{}, err
}
kdfRes, err := KDF(headerInitials[len(headerInitials)-1].SecretHash)
if err != nil {
return Header{}, err
}
encFinalHop, err := AesCtr(kdfRes, finalHopBytes)
if err != nil {
errMsg := fmt.Errorf("error in encapsulateHeader - AES_CTR encryption failed: %v", err)
return Header{}, errMsg
}
mac, err := computeMac(kdfRes, encFinalHop)
if err != nil {
return Header{}, err
}
routingCommands := [][]byte{encFinalHop}
var encRouting []byte
for i := len(nodes) - 2; i >= 0; i-- {
nextNode := nodes[i+1]
routing := RoutingInfo{NextHop: &Hop{Id: nextNode.Id,
Address: nextNode.Host + ":" + nextNode.Port,
PubKey: nodes[i+1].PubKey,
}, RoutingCommands: &commands[i],
NextHopMetaData: routingCommands[len(routingCommands)-1],
Mac: mac,
}
encKey, err := KDF(headerInitials[i].SecretHash)
if err != nil {
return Header{}, err
}
routingBytes, err := proto.Marshal(&routing)
if err != nil {
return Header{}, err
}
encRouting, err = AesCtr(encKey, routingBytes)
if err != nil {
return Header{}, err
}
routingCommands = append(routingCommands, encRouting)
kdfResL, err := KDF(headerInitials[i].SecretHash)
if err != nil {
return Header{}, nil
}
mac, err = computeMac(kdfResL, encRouting)
if err != nil {
return Header{}, err
}
}
return Header{Alpha: headerInitials[0].Alpha, Beta: encRouting, Mac: mac}, nil
}
// encapsulateContent layer encrypts the given messages using a set of shared keys
// and the AES_CTR encryption.
// encapsulateContent returns the encrypted payload in byte representation. If the AES_CTR
// encryption failed encapsulateContent returns an error.
func encapsulateContent(headerInitials []HeaderInitials, message []byte) ([]byte, error) {
enc := message
for i := len(headerInitials) - 1; i >= 0; i-- {
sharedKey, err := KDF(headerInitials[i].SecretHash)
if err != nil {
return nil, err
}
enc, err = AesCtr(sharedKey, enc)
if err != nil {
errMsg := fmt.Errorf("error in encapsulateContent - AES_CTR encryption failed: %v", err)
return nil, errMsg
}
}
return enc, nil
}
// getSharedSecrets computes a sequence of HeaderInitial values, containing the initial elements,
// shared secrets and blinding factors for each node on the path. As input getSharedSecrets takes the initial
// secret value, the list of nodes, and the curve in which the cryptographic operations are performed.
// getSharedSecrets returns the list of computed HeaderInitials or an error.
func getSharedSecrets(nodes []config.MixConfig, initialVal *FieldElement) ([]HeaderInitials, error) {
blindFactors := []*FieldElement{initialVal}
tuples := make([]HeaderInitials, len(nodes))
for i, n := range nodes {
// initial implementation:
// for x1, x2, ... xn in blindFactors:
// compute tmp := x1 * x2 * ... xn
// return g^tmp
// replacing to:
// for x1, x2, ... xn in blindFactors:
// compute tmp1 := g^x1
// tmp2 := tmp1^x2
// ...
// return tmp{n-1}^xn
alpha := expoGroupBase(blindFactors)
if len(n.PubKey) != PublicKeySize {
errMsg := fmt.Errorf("invalid public key provided for node %v", i)
return nil, errMsg
}
// initial implementation:
// for x1, x2, ... xn in blindFactors:
// compute tmp := x1 * x2 * ... xn
// return base^tmp
// replacing to:
// for x1, x2, ... xn in blindFactors:
// compute tmp1 := base^x1
// tmp2 := tmp1^x2
// ...
// return tmpn-1^xn
s := expo(BytesToPublicKey(n.PubKey).ToFieldElement(), blindFactors)
// TODO: move to the other crypto file?
aesS, err := KDF(s.Bytes())
if err != nil {
return nil, err
}
blinder, err := computeBlindingFactor(aesS)
if err != nil {
errMsg := fmt.Errorf("error in getSharedSecrets - computeBlindingFactor failed: %v", err)
return nil, errMsg
}
blindFactors = append(blindFactors, blinder)
tuples[i] = HeaderInitials{Alpha: alpha.Bytes(), Secret: s.Bytes(), Blinder: blinder.Bytes(), SecretHash: aesS}
}
return tuples, nil
}
// TODO: computeFillers needs to be fixed
func computeFillers(nodes []config.MixConfig, tuples []HeaderInitials) (string, error) {
filler := ""
minLen := headerLength - 32
for i := 1; i < len(nodes); i++ {
base := filler + strings.Repeat("\x00", K)
kx, err := computeSharedSecretHash(tuples[i-1].SecretHash, []byte("hrhohrhohrhohrho"))
if err != nil {
return "", err
}
mx := strings.Repeat("\x00", minLen) + base
xorVal, err := AesCtr(kx, []byte(mx))
if err != nil {
errMsg := fmt.Errorf("error in computeFillers - AES_CTR failed: %v", err)
return "", errMsg
}
filler = BytesToString(xorVal)
filler = filler[minLen:]
minLen -= K
}
return filler, nil
}
// computeBlindingFactor computes the blinding factor extracted from the
// shared secrets. Blinding factors allow both the sender and intermediate nodes
// recompute the shared keys used at each hop of the message processing.
// computeBlindingFactor returns a value of a blinding factor or an error.
func computeBlindingFactor(key []byte) (*FieldElement, error) {
iv := []byte("initialvector000")
blinderBytes, err := computeSharedSecretHash(key, iv)
if err != nil {
errMsg := fmt.Errorf("error in computeBlindingFactor - computeSharedSecretHash failed: %v", err)
return nil, errMsg
}
return BytesToFieldElement(blinderBytes), nil
}
// computeSharedSecretHash computes the hash value of the shared secret key
// using AES_CTR.
func computeSharedSecretHash(key []byte, iv []byte) ([]byte, error) {
aesCipher, err := aes.NewCipher(key)
if err != nil {
errMsg := fmt.Errorf("error in computeSharedSecretHash - creating new AES cipher failed: %v", err)
return nil, errMsg
}
stream := cipher.NewCTR(aesCipher, iv)
plaintext := []byte("0000000000000000")
ciphertext := make([]byte, len(plaintext))
stream.XORKeyStream(ciphertext, plaintext)
return ciphertext, nil
}
// ProcessSphinxPacket processes the sphinx packet using the given private key.
// ProcessSphinxPacket unwraps one layer of both the header and the payload encryption.
// ProcessSphinxPacket returns a new packet and the routing information which should
// be used by the processing node. If any cryptographic or parsing operation failed ProcessSphinxPacket
// returns an error.
func ProcessSphinxPacket(packetBytes []byte, privKey *PrivateKey) (Hop, Commands, []byte, error) {
var packet SphinxPacket
err := proto.Unmarshal(packetBytes, &packet)
if err != nil {
errMsg := fmt.Errorf("error in ProcessSphinxPacket - unmarshal of packet failed: %v", err)
return Hop{}, Commands{}, nil, errMsg
}
hop, commands, newHeader, err := ProcessSphinxHeader(*packet.Hdr, privKey)
if err != nil {
errMsg := fmt.Errorf("error in ProcessSphinxPacket - ProcessSphinxHeader failed: %v", err)
return Hop{}, Commands{}, nil, errMsg
}
newPayload, err := ProcessSphinxPayload(packet.Hdr.Alpha, packet.Pld, privKey)
if err != nil {
errMsg := fmt.Errorf("error in ProcessSphinxPacket - ProcessSphinxPayload failed: %v", err)
return Hop{}, Commands{}, nil, errMsg
}
newPacket := SphinxPacket{Hdr: &newHeader, Pld: newPayload}
newPacketBytes, err := proto.Marshal(&newPacket)
if err != nil {
errMsg := fmt.Errorf("error in ProcessSphinxPacket - marshal of packet failed: %v", err)
return Hop{}, Commands{}, nil, errMsg
}
return hop, commands, newPacketBytes, nil
}
// ProcessSphinxHeader unwraps one layer of encryption from the header of a sphinx packet.
// ProcessSphinxHeader recomputes the shared key and checks whether the message authentication code is valid.
// If not, the packet is dropped and error is returned. If MAC checking was passed successfully ProcessSphinxHeader
// performs the AES_CTR decryption, recomputes the blinding factor and updates the init public element from the header.
// Next, ProcessSphinxHeader extracts the routing information from the decrypted packet and returns it,
// together with the updated init public element.
// If any crypto or parsing operation failed ProcessSphinxHeader returns an error.
func ProcessSphinxHeader(packet Header, privKey *PrivateKey) (Hop, Commands, Header, error) {
alpha := BytesToFieldElement(packet.Alpha)
beta := packet.Beta
mac := packet.Mac
sharedSecret := new(FieldElement)
curve25519.ScalarMult(sharedSecret.el(), privKey.ToFieldElement().el(), alpha.el())
aesS, err := KDF(sharedSecret.Bytes())
if err != nil {
return Hop{}, Commands{}, Header{}, err
}
encKey, err := KDF(aesS)
if err != nil {
return Hop{}, Commands{}, Header{}, err
}
recomputedMac, err := computeMac(encKey, beta)
if err != nil {
return Hop{}, Commands{}, Header{}, err
}
if !bytes.Equal(recomputedMac, mac) {
return Hop{}, Commands{}, Header{}, errors.New("packet processing error: MACs are not matching")
}
blinder, err := computeBlindingFactor(aesS)
if err != nil {
errMsg := fmt.Errorf("error in ProcessSphinxHeader - computeBlindingFactor failed: %v", err)
return Hop{}, Commands{}, Header{}, errMsg
}
newAlpha := new(FieldElement)
curve25519.ScalarMult(newAlpha.el(), blinder.el(), alpha.el())
decBeta, err := AesCtr(encKey, beta)
if err != nil {
errMsg := fmt.Errorf("error in ProcessSphinxHeader - AES_CTR failed: %v", err)
return Hop{}, Commands{}, Header{}, errMsg
}
var routingInfo RoutingInfo
err = proto.Unmarshal(decBeta, &routingInfo)
if err != nil {
errMsg := fmt.Errorf("error in ProcessSphinxHeader - unmarshal of beta failed: %v", err)
return Hop{}, Commands{}, Header{}, errMsg
}
nextHop, commands, nextBeta, nextMac := readBeta(routingInfo)
return nextHop, commands, Header{Alpha: newAlpha.Bytes(), Beta: nextBeta, Mac: nextMac}, nil
}
// readBeta extracts all the fields from the RoutingInfo structure
func readBeta(beta RoutingInfo) (Hop, Commands, []byte, []byte) {
nextHop := *beta.NextHop
commands := *beta.RoutingCommands
nextBeta := beta.NextHopMetaData
nextMac := beta.Mac
return nextHop, commands, nextBeta, nextMac
}
// ProcessSphinxPayload unwraps a single layer of the encryption from the sphinx packet payload.
// ProcessSphinxPayload first recomputes the shared secret which is used to perform the AES_CTR decryption.
// ProcessSphinxPayload returns the new packet payload or an error if the decryption failed.
func ProcessSphinxPayload(alpha []byte, payload []byte, privKey *PrivateKey) ([]byte, error) {
sharedSecret := new(FieldElement)
curve25519.ScalarMult(sharedSecret.el(), privKey.ToFieldElement().el(), BytesToFieldElement(alpha).el())
aesS, err := KDF(sharedSecret.Bytes())
if err != nil {
return nil, err
}
decKey, err := KDF(aesS)
if err != nil {
return nil, err
}
decPayload, err := AesCtr(decKey, payload)
if err != nil {
errMsg := fmt.Errorf("error in ProcessSphinxPayload - AES_CTR decryption failed: %v", err)
return nil, errMsg
}
return decPayload, nil
}