Skip to content

Commit 9b2478b

Browse files
committed
add attributable error encryption and decryption
1 parent 78880cd commit 9b2478b

7 files changed

+1005
-0
lines changed

attributable_error_crypto.go

+154
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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+
// hmacSize is the number of bytes that is reserved for each hmac.
33+
hmacSize int
34+
35+
zeroHmac []byte
36+
}
37+
38+
func NewAttrErrorStructure(hopCount int, fixedPayloadLen int,
39+
hmacBytes int) *AttrErrorStructure {
40+
41+
return &AttrErrorStructure{
42+
hopCount: hopCount,
43+
fixedPayloadLen: fixedPayloadLen,
44+
hmacSize: hmacBytes,
45+
46+
zeroHmac: make([]byte, hmacBytes),
47+
}
48+
}
49+
50+
// totalHmacs is the total number of hmacs that is present in the failure
51+
// message. Every hop adds HopCount hmacs to the message, but as the error
52+
// back-propagates, downstream hmacs can be pruned. This results in the number
53+
// of hmacs for each hop decreasing by one for each step that we move away from
54+
// the current node.
55+
func (o *AttrErrorStructure) totalHmacs() int {
56+
return (o.hopCount * (o.hopCount + 1)) / 2
57+
}
58+
59+
// allHmacsLen is the total length in the bytes of all hmacs in the failure
60+
// message.
61+
func (o *AttrErrorStructure) allHmacsLen() int {
62+
return o.totalHmacs() * o.hmacSize
63+
}
64+
65+
// hmacsAndPayloadsLen is the total length in bytes of all hmacs and payloads
66+
// together.
67+
func (o *AttrErrorStructure) hmacsAndPayloadsLen() int {
68+
return o.allHmacsLen() + o.allPayloadsLen()
69+
}
70+
71+
// allPayloadsLen is the total length in bytes of all payloads in the failure
72+
// message.
73+
func (o *AttrErrorStructure) allPayloadsLen() int {
74+
return o.payloadLen() * o.hopCount
75+
}
76+
77+
// payloadLen is the size of the per-node payload. It consists of a 1-byte
78+
// payload type followed by the payload data.
79+
func (o *AttrErrorStructure) payloadLen() int {
80+
return 1 + o.fixedPayloadLen
81+
}
82+
83+
// message returns a slice containing the message in the given failure data
84+
// block. The message is positioned at the beginning of the block.
85+
func (o *AttrErrorStructure) message(data []byte) []byte {
86+
return data[:len(data)-o.hmacsAndPayloadsLen()]
87+
}
88+
89+
// payloads returns a slice containing all payloads in the given failure
90+
// data block. The payloads follow the message in the block.
91+
func (o *AttrErrorStructure) payloads(data []byte) []byte {
92+
dataLen := len(data)
93+
94+
return data[dataLen-o.hmacsAndPayloadsLen() : dataLen-o.allHmacsLen()]
95+
}
96+
97+
// hmacs returns a slice containing all hmacs in the given failure data block.
98+
// The hmacs are positioned at the end of the data block.
99+
func (o *AttrErrorStructure) hmacs(data []byte) []byte {
100+
return data[len(data)-o.allHmacsLen():]
101+
}
102+
103+
// calculateHmac calculates an hmac given a shared secret and a presumed
104+
// position in the path. Position is expressed as the distance to the error
105+
// source. The error source itself is at position 0.
106+
func (o *AttrErrorStructure) calculateHmac(sharedSecret Hash256,
107+
position int, message, payloads, hmacs []byte) []byte {
108+
109+
umKey := generateKey("um", &sharedSecret)
110+
hash := hmac.New(sha256.New, umKey[:])
111+
112+
// Include message.
113+
_, _ = hash.Write(message)
114+
115+
// Include payloads including our own.
116+
_, _ = hash.Write(payloads[:(position+1)*o.payloadLen()])
117+
118+
// Include downstream hmacs.
119+
writeDownstreamHmacs(position, o.hopCount, hmacs, o.hmacSize, hash)
120+
121+
hmac := hash.Sum(nil)
122+
123+
return hmac[:o.hmacSize]
124+
}
125+
126+
// writeDownstreamHmacs writes the hmacs of downstream nodes that are relevant
127+
// for the given position to a writer instance. Position is expressed as the
128+
// distance to the error source. The error source itself is at position 0.
129+
func writeDownstreamHmacs(position, maxHops int, hmacs []byte, hmacBytes int,
130+
w io.Writer) {
131+
132+
// Track the index of the next hmac to write in a variable. The first
133+
// maxHops slots are reserved for the hmacs of the current hop and can
134+
// therefore be skipped. The first hmac to write is part of the block of
135+
// hmacs that was written by the first downstream node. Which hmac
136+
// exactly is determined by the assumed position of the current node.
137+
var hmacIdx = maxHops + (maxHops - position - 1)
138+
139+
// Iterate over all downstream nodes.
140+
for j := 0; j < position; j++ {
141+
_, _ = w.Write(
142+
hmacs[hmacIdx*hmacBytes : (hmacIdx+1)*hmacBytes],
143+
)
144+
145+
// Calculate the total number of hmacs in the block of the
146+
// current downstream node.
147+
blockSize := maxHops - j - 1
148+
149+
// Skip to the next block. The new hmac index will point to the
150+
// hmac that corresponds to the next downstream node which is
151+
// one step closer to the assumed error source.
152+
hmacIdx += blockSize
153+
}
154+
}

0 commit comments

Comments
 (0)