Skip to content

Commit d70c20f

Browse files
committed
sphinx: get rid of mutating CalculateRealm method in favor of packRealm
Rather than mutate the underlying realm byte, we'll now expose two methods to the caller: one for the "external" realm, and one of the realm that we'll encode in the payload.
1 parent b175ee5 commit d70c20f

File tree

2 files changed

+29
-20
lines changed

2 files changed

+29
-20
lines changed

path.go

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,19 @@ func NewHopPayload(realm byte, hopData *HopData, eob []byte) (HopPayload, error)
160160
func (hp *HopPayload) Realm() byte {
161161
return hp.realm[0] & RealmMaskBytes
162162
}
163+
164+
// payloadRealm returns the realm that will be used within the raw packed hop
165+
// payload. This differs from the Realm method above in that it uses space to
166+
// encode the packet type and number of frames. The final encoding uses the
167+
// first 4 bits of the realm to encode the number of frames used, and the
168+
// latter 4 bits to encode the real realm type.
169+
func (hp *HopPayload) payloadRealm() byte {
170+
maskedRealm := hp.realm[0] & 0x0F
171+
numFrames := hp.NumFrames()
172+
173+
return maskedRealm | (byte(numFrames-1) << NumFramesShift)
174+
}
175+
163176
// NumFrames returns the total number of frames it'll take to pack the target
164177
// HopPayload into a Sphinx packet.
165178
func (hp *HopPayload) NumFrames() int {
@@ -177,34 +190,29 @@ func (hp *HopPayload) NumFrames() int {
177190
return 2 + int(math.Ceil(float64(remainder)/65))
178191
}
179192

180-
// CalculateRealm computes the proper realm encoding in place. The final
181-
// encoding uses the first 4 bits of the realm to encode the number of frames
182-
// used, and the latter 4 bits to encode the real realm type.
183-
func (hp *HopPayload) CalculateRealm() {
184-
maskedRealm := hp.Realm[0] & 0x0F
185-
numFrames := hp.NumFrames()
193+
// packRealm writes out the proper realm encoding in place to the target
194+
// io.Writer.
195+
func (hp *HopPayload) packRealm(w io.Writer) error {
196+
realm := hp.payloadRealm()
197+
if _, err := w.Write([]byte{realm}); err != nil {
198+
return err
199+
}
186200

187-
hp.Realm[0] = maskedRealm | (byte(numFrames-1) << NumFramesShift)
201+
return nil
188202
}
189203

190204
// Encode encodes the hop payload into the passed writer.
191205
func (hp *HopPayload) Encode(w io.Writer) error {
192206
// We'll need to add enough padding bytes to position the HMAC at the
193207
// end of the payload
194-
padding := hp.NumFrames()*65 - len(hp.Payload) - 1 - 32
208+
padding := hp.NumFrames()*FrameSize - len(hp.Payload) - 1 - HMACSize
195209
if padding < 0 {
196210
return fmt.Errorf("cannot have negative padding: %v", padding)
197211
}
198212

199-
// Before we write the realm out, we need to calculate the current
200-
// realm based on the "true" realm as well as the number of frames it
201-
// takes to compute the hop payload.
202-
hp.CalculateRealm()
203-
204-
if _, err := w.Write(hp.Realm[:]); err != nil {
213+
if err := hp.packRealm(w); err != nil {
205214
return err
206215
}
207-
208216
if _, err := w.Write(hp.Payload); err != nil {
209217
return err
210218
}
@@ -228,11 +236,11 @@ func (hp *HopPayload) Encode(w io.Writer) error {
228236
// Decode unpacks an encoded HopPayload from the passed reader into the target
229237
// HopPayload.
230238
func (hp *HopPayload) Decode(r io.Reader) error {
231-
if _, err := io.ReadFull(r, hp.Realm[:]); err != nil {
239+
if _, err := io.ReadFull(r, hp.realm[:]); err != nil {
232240
return err
233241
}
234242

235-
numFrames := int(hp.Realm[0]>>NumFramesShift) + 1
243+
numFrames := int(hp.realm[0]>>NumFramesShift) + 1
236244
numBytes := (numFrames * FrameSize) - 32 - 1
237245

238246
hp.Payload = make([]byte, numBytes)

path_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ func TestHopPayloadSizes(t *testing.T) {
3434
"%d, actual %d", tt.expected, actual)
3535
}
3636

37-
hp.CalculateRealm()
38-
if hp.Realm[0] != tt.realm {
39-
t.Errorf("Updated realm did not match our expectation: expected %q, actual %q", tt.realm, hp.Realm)
37+
if hp.payloadRealm() != tt.realm {
38+
t.Errorf("payload realm did not match our "+
39+
"expectation: expected %q, actual %q", tt.realm,
40+
hp.Realm())
4041
}
4142
}
4243
}

0 commit comments

Comments
 (0)