@@ -20,13 +20,13 @@ const (
2020 // during onion creation as well as during the verification.
2121 HMACSize = 32
2222
23- // AMMAG is the string representation for the ammag key type. Used in
23+ // Ammag is the string representation for the ammag key type. Used in
2424 // cypher stream generation.
25- AMMAG = "ammag"
25+ Ammag = "ammag"
2626
27- // AMMAG_EXT is the string representation for the extended ammag key
27+ // AmmagExt is the string representation for the extended ammag key
2828 // type. Used in cypher stream generation.
29- AMMAG_EXT = "ammagext"
29+ AmmagExt = "ammagext"
3030)
3131
3232// chaChaPolyZeroNonce is a slice of zero bytes used in the chacha20poly1305
@@ -371,9 +371,9 @@ func (o *OnionErrorDecrypter) DecryptError(encryptedData []byte,
371371 Sender : o .circuit .PaymentPath [0 ],
372372 SenderIdx : 1 ,
373373 }, nil
374- } else {
375- validAttr = false
376374 }
375+
376+ validAttr = false
377377 }
378378
379379 sharedSecrets , _ , err := generateSharedSecrets (
@@ -396,7 +396,7 @@ func (o *OnionErrorDecrypter) DecryptError(encryptedData []byte,
396396 failData := make ([]byte , len (encryptedData ))
397397 copy (failData , encryptedData )
398398
399- hopPayloads := make ([]uint32 , 0 )
399+ holdTimes := make ([]uint32 , 0 )
400400
401401 // We'll iterate a constant amount of hops to ensure that we don't give
402402 // away an timing information pertaining to the position in the route
@@ -418,13 +418,13 @@ func (o *OnionErrorDecrypter) DecryptError(encryptedData []byte,
418418 // encryption from the encrypted failure and attribution
419419 // data. This needs to be done before parsing the attribution
420420 // data, as the attribution data HMACs commit to it.
421- failData = onionEncrypt (AMMAG , & sharedSecret , failData )
421+ failData = onionEncrypt (Ammag , & sharedSecret , failData )
422422
423423 // If the attribution data are valid then do another round of
424424 // attribution data decryption.
425425 if validAttr {
426426 attrData = onionEncrypt (
427- AMMAG_EXT , & sharedSecret , attrData ,
427+ AmmagExt , & sharedSecret , attrData ,
428428 )
429429
430430 payloads := o .payloads (attrData )
@@ -447,12 +447,10 @@ func (o *OnionErrorDecrypter) DecryptError(encryptedData []byte,
447447 if ! bytes .Equal (actualAttrHmac , expectedAttrHmac ) &&
448448 sender == 0 && i < len (o .circuit .PaymentPath ) {
449449
450- switch strictAttribution {
451- case true :
450+ if strictAttribution {
452451 sender = i + 1
453452 msg = nil
454-
455- case false :
453+ } else {
456454 // Flag the attribution data as invalid
457455 // from this point onwards. This will
458456 // prevent the loop from trying to
@@ -462,12 +460,13 @@ func (o *OnionErrorDecrypter) DecryptError(encryptedData []byte,
462460 }
463461 }
464462
465- // Extract the payload and exit with a nil message if it
466- // is invalid.
467- holdTime := o .extractPayload (payloads )
463+ // Extract hold time from the payload.
464+ holdTime := binary .BigEndian .Uint32 (
465+ payloads [:o .fixedPayloadLen ],
466+ )
468467 if sender == 0 && validAttr {
469468 // Store hold time reported by this node.
470- hopPayloads = append (hopPayloads , holdTime )
469+ holdTimes = append (holdTimes , holdTime )
471470
472471 // Update the message.
473472 msg = failData [sha256 .Size :]
@@ -509,19 +508,12 @@ func (o *OnionErrorDecrypter) DecryptError(encryptedData []byte,
509508 Sender : o .circuit .PaymentPath [sender - 1 ],
510509 SenderIdx : sender ,
511510 Message : msg ,
512- HoldTimes : hopPayloads ,
511+ HoldTimes : holdTimes ,
513512 }, nil
514513}
515514
516- // extractPayload extracts the payload and payload origin information from the
517- // given byte slice.
518- func (o * OnionErrorDecrypter ) extractPayload (payloadBytes []byte ) uint32 {
519- // Extract payload.
520- holdTime := binary .BigEndian .Uint32 (payloadBytes [0 :o .payloadLen ()])
521-
522- return holdTime
523- }
524-
515+ // shiftPayloadsLeft shifts all payloads one position to the left, preparing
516+ // for the next decryption iteration.
525517func (o * OnionErrorDecrypter ) shiftPayloadsLeft (payloads []byte ) {
526518 copy (payloads , payloads [o .payloadLen ():o .hopCount * o .payloadLen ()])
527519}
@@ -608,19 +600,21 @@ func (o *OnionErrorEncrypter) EncryptError(initial bool, legacyData []byte,
608600 // Update hmac block.
609601 o .addHmacs (attrData , legacyData )
610602
611- legacy := onionEncrypt (AMMAG , & o .sharedSecret , legacyData )
612- attrError := onionEncrypt (AMMAG_EXT , & o .sharedSecret , attrData )
603+ legacy := onionEncrypt (Ammag , & o .sharedSecret , legacyData )
604+ attrError := onionEncrypt (AmmagExt , & o .sharedSecret , attrData )
613605
614606 return legacy , attrError , nil
615607}
616608
609+ // shiftHmacsRight shifts all hmacs one position to the right, making room for
610+ // new hmacs at the front for the current hop.
617611func (o * OnionErrorEncrypter ) shiftHmacsRight (hmacs []byte ) {
618- totalHmacs := ( o . hopCount * ( o . hopCount + 1 )) / 2
612+ total := o . totalHmacs ()
619613
620614 // Work from right to left to avoid overwriting data that is still
621615 // needed.
622- srcIdx := totalHmacs - 2
623- destIdx := totalHmacs - 1
616+ srcIdx := total - 2
617+ destIdx := total - 1
624618
625619 // The variable copyLen contains the number of hmacs to copy for the
626620 // current hop.
@@ -669,8 +663,9 @@ func (o *OnionErrorEncrypter) addHmacs(data []byte, message []byte) {
669663 }
670664}
671665
666+ // initializePayload creates a new attribution data block with the given hold
667+ // time set as the initial payload.
672668func (o * OnionErrorEncrypter ) initializePayload (holdTime uint32 ) []byte {
673-
674669 // Add space for payloads and hmacs.
675670 data := make ([]byte , o .hmacsAndPayloadsLen ())
676671
@@ -682,6 +677,8 @@ func (o *OnionErrorEncrypter) initializePayload(holdTime uint32) []byte {
682677 return data
683678}
684679
680+ // addIntermediatePayload shifts existing payloads and prepends the given hold
681+ // time as the new first payload entry.
685682func (o * OnionErrorEncrypter ) addIntermediatePayload (data []byte ,
686683 holdTime uint32 ) {
687684
@@ -694,10 +691,13 @@ func (o *OnionErrorEncrypter) addIntermediatePayload(data []byte,
694691 addPayload (payloads , holdTime )
695692}
696693
694+ // shiftPayloadsRight shifts all payloads one position to the right, making
695+ // room for a new payload at the front.
697696func (o * OnionErrorEncrypter ) shiftPayloadsRight (payloads []byte ) {
698697 copy (payloads [o .payloadLen ():], payloads )
699698}
700699
700+ // addPayload writes a hold time value into the first payload slot.
701701func addPayload (payloads []byte , holdTime uint32 ) {
702702 binary .BigEndian .PutUint32 (payloads , holdTime )
703703}
0 commit comments