Skip to content

Commit 09db49b

Browse files
committed
add fat error encryption and decryption
1 parent 78880cd commit 09db49b

7 files changed

+976
-0
lines changed

attributable_error_crypto.go

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package sphinx
2+
3+
import (
4+
"crypto/hmac"
5+
"crypto/sha256"
6+
"io"
7+
)
8+
9+
type payloadSource byte
10+
11+
const (
12+
// payloadIntermediateNode is a marker to signal that this attributable
13+
// error payload is originating from a node between the payer and the
14+
// error source.
15+
payloadIntermediateNode payloadSource = 0
16+
17+
// payloadErrorNode is a marker to signal that this attributable error
18+
// payload is originating from the error source.
19+
payloadErrorNode payloadSource = 1
20+
)
21+
22+
// AttrErrorStructure contains the parameters that define the structure
23+
// of the error message that is passed back.
24+
type AttrErrorStructure struct {
25+
// HopCount is the assumed maximum number of hops in the path.
26+
HopCount int
27+
28+
// FixedPayloadLen is the length of the payload data that each hop along
29+
// the route can add.
30+
FixedPayloadLen int
31+
}
32+
33+
func (o *AttrErrorStructure) maxHops() int {
34+
return o.HopCount
35+
}
36+
37+
func (o *AttrErrorStructure) payloadDataLen() int {
38+
return o.FixedPayloadLen
39+
}
40+
41+
// totalHmacs is the total number of hmacs that is present in the failure
42+
// message. Every hop adds HopCount hmacs to the message, but as the error
43+
// back-propagates, downstream hmacs can be pruned. This results in the number
44+
// of hmacs for each hop decreasing by one for each step that we move away from
45+
// the current node.
46+
func (o *AttrErrorStructure) totalHmacs() int {
47+
return (o.HopCount * (o.HopCount + 1)) / 2
48+
}
49+
50+
// allHmacsLen is the total length in the bytes of all hmacs in the failure
51+
// message.
52+
func (o *AttrErrorStructure) allHmacsLen() int {
53+
return o.totalHmacs() * sha256.Size
54+
}
55+
56+
// hmacsAndPayloadsLen is the total length in bytes of all hmacs and payloads
57+
// together.
58+
func (o *AttrErrorStructure) hmacsAndPayloadsLen() int {
59+
return o.allHmacsLen() + o.allPayloadsLen()
60+
}
61+
62+
// allPayloadsLen is the total length in bytes of all payloads in the failure
63+
// message.
64+
func (o *AttrErrorStructure) allPayloadsLen() int {
65+
return o.payloadLen() * o.HopCount
66+
}
67+
68+
// payloadLen is the size of the per-node payload. It consists of a 1-byte
69+
// payload type followed by the payload data.
70+
func (o *AttrErrorStructure) payloadLen() int {
71+
return 1 + o.payloadDataLen()
72+
}
73+
74+
// message returns a slice containing the message in the given failure data
75+
// block. The message is positioned at the beginning of the block.
76+
func (o *AttrErrorStructure) message(data []byte) []byte {
77+
return data[:len(data)-o.hmacsAndPayloadsLen()]
78+
}
79+
80+
// payloads returns a slice containing all payloads in the given failure
81+
// data block. The payloads follow the message in the block.
82+
func (o *AttrErrorStructure) payloads(data []byte) []byte {
83+
dataLen := len(data)
84+
85+
return data[dataLen-o.hmacsAndPayloadsLen() : dataLen-o.allHmacsLen()]
86+
}
87+
88+
// hmacs returns a slice containing all hmacs in the given failure data block.
89+
// The hmacs are positioned at the end of the data block.
90+
func (o *AttrErrorStructure) hmacs(data []byte) []byte {
91+
return data[len(data)-o.allHmacsLen():]
92+
}
93+
94+
// calculateHmac calculates an hmac given a shared secret and a presumed
95+
// position in the path. Position is expressed as the distance to the error
96+
// source. The error source itself is at position 0.
97+
func (o *AttrErrorStructure) calculateHmac(sharedSecret Hash256,
98+
position int, message, payloads, hmacs []byte) []byte {
99+
100+
umKey := generateKey("um", &sharedSecret)
101+
hash := hmac.New(sha256.New, umKey[:])
102+
103+
// Include message.
104+
_, _ = hash.Write(message)
105+
106+
// Include payloads including our own.
107+
_, _ = hash.Write(payloads[:(o.maxHops()-position)*o.payloadLen()])
108+
109+
// Include downstream hmacs.
110+
writeDownstreamHmacs(position, o.maxHops(), hmacs, hash)
111+
112+
return hash.Sum(nil)
113+
}
114+
115+
// writeDownstreamHmacs writes the hmacs of downstream nodes that are relevant
116+
// for the given position to a writer instance.
117+
func writeDownstreamHmacs(position, maxHops int, hmacs []byte, w io.Writer) {
118+
// Track the index of the next hmac to write in a variable. The first
119+
// maxHops slots are reserved for the hmacs of the current hop and can
120+
// therefore be skipped. The first hmac to write is part of the block of
121+
// hmacs that was written by the first downstream node. Which hmac
122+
// exactly is determined by the assumed position of the current node.
123+
var hmacIdx = maxHops + position
124+
125+
// Iterate over all downstream nodes.
126+
for j := 0; j < maxHops-position-1; j++ {
127+
_, _ = w.Write(
128+
hmacs[hmacIdx*sha256.Size : (hmacIdx+1)*sha256.Size],
129+
)
130+
131+
// Calculate the total number of hmacs in the block of the
132+
// current downstream node.
133+
blockSize := maxHops - j - 1
134+
135+
// Skip to the next block. The new hmac index will point to the
136+
// hmac that corresponds to the next downstream node which is
137+
// one step closer to the assumed error source.
138+
hmacIdx += blockSize
139+
}
140+
}

0 commit comments

Comments
 (0)