16
16
17
17
#define BLINDING_FACTOR_SIZE 32
18
18
19
- #define ONION_REPLY_SIZE 256
20
-
21
19
#define RHO_KEYTYPE "rho"
22
20
23
21
struct hop_params {
@@ -680,23 +678,35 @@ struct route_step *process_onionpacket(
680
678
}
681
679
682
680
#if DEVELOPER
683
- unsigned dev_onion_reply_length = ONION_REPLY_SIZE ;
684
- #define OUR_ONION_REPLY_SIZE dev_onion_reply_length
685
- #else
686
- #define OUR_ONION_REPLY_SIZE ONION_REPLY_SIZE
681
+ unsigned dev_onion_reply_length = 256 ;
687
682
#endif
688
683
689
684
struct onionreply * create_onionreply (const tal_t * ctx ,
690
685
const struct secret * shared_secret ,
691
686
const u8 * failure_msg )
692
687
{
693
688
size_t msglen = tal_count (failure_msg );
694
- size_t padlen = OUR_ONION_REPLY_SIZE - msglen ;
689
+ size_t padlen ;
695
690
struct onionreply * reply = tal (ctx , struct onionreply );
696
691
u8 * payload = tal_arr (ctx , u8 , 0 );
697
692
struct secret key ;
698
693
struct hmac hmac ;
699
694
695
+ /* BOLT #4:
696
+ * The _erring node_:
697
+ * - SHOULD set `pad` such that the `failure_len` plus `pad_len`
698
+ * is equal to 256.
699
+ * - Note: this value is 118 bytes longer than the longest
700
+ * currently-defined message.
701
+ */
702
+ const u16 onion_reply_size = IFDEV (dev_onion_reply_length , 256 );
703
+
704
+ /* We never do this currently, but could in future! */
705
+ if (msglen > onion_reply_size )
706
+ padlen = 0 ;
707
+ else
708
+ padlen = onion_reply_size - msglen ;
709
+
700
710
/* BOLT #4:
701
711
*
702
712
* The node generating the error message (_erring node_) builds a return
@@ -715,15 +725,8 @@ struct onionreply *create_onionreply(const tal_t *ctx,
715
725
towire_u16 (& payload , padlen );
716
726
towire_pad (& payload , padlen );
717
727
718
- /* BOLT #4:
719
- *
720
- * The _erring node_:
721
- * - SHOULD set `pad` such that the `failure_len` plus `pad_len` is
722
- * equal to 256.
723
- * - Note: this value is 118 bytes longer than the longest
724
- * currently-defined message.
725
- */
726
- assert (tal_count (payload ) == OUR_ONION_REPLY_SIZE + 4 );
728
+ /* Two bytes for each length: failure_len and pad_len */
729
+ assert (tal_count (payload ) == onion_reply_size + 4 );
727
730
728
731
/* BOLT #4:
729
732
*
@@ -770,52 +773,47 @@ u8 *unwrap_onionreply(const tal_t *ctx,
770
773
int * origin_index )
771
774
{
772
775
struct onionreply * r ;
773
- struct secret key ;
774
- struct hmac hmac ;
775
776
const u8 * cursor ;
776
- u8 * final ;
777
777
size_t max ;
778
778
u16 msglen ;
779
779
780
- if (tal_count (reply -> contents ) != ONION_REPLY_SIZE + sizeof (hmac ) + 4 ) {
781
- return NULL ;
782
- }
783
-
784
780
r = new_onionreply (tmpctx , reply -> contents );
785
781
* origin_index = -1 ;
786
782
787
783
for (int i = 0 ; i < numhops ; i ++ ) {
784
+ struct secret key ;
785
+ struct hmac hmac , expected_hmac ;
786
+
788
787
/* Since the encryption is just XORing with the cipher
789
788
* stream encryption is identical to decryption */
790
789
r = wrap_onionreply (tmpctx , & shared_secrets [i ], r );
791
790
792
791
/* Check if the HMAC matches, this means that this is
793
792
* the origin */
794
793
subkey_from_hmac ("um" , & shared_secrets [i ], & key );
795
- compute_hmac (& key , r -> contents + sizeof (hmac .bytes ),
796
- tal_count (r -> contents ) - sizeof (hmac .bytes ),
797
- NULL , 0 , & hmac );
798
- if (memcmp (hmac .bytes , r -> contents , sizeof (hmac .bytes )) == 0 ) {
794
+
795
+ cursor = r -> contents ;
796
+ max = tal_count (r -> contents );
797
+
798
+ fromwire_hmac (& cursor , & max , & hmac );
799
+ /* Too short. */
800
+ if (!cursor )
801
+ return NULL ;
802
+
803
+ compute_hmac (& key , cursor , max , NULL , 0 , & expected_hmac );
804
+ if (hmac_eq (& hmac , & expected_hmac )) {
799
805
* origin_index = i ;
800
806
break ;
801
807
}
802
808
}
809
+
810
+ /* Didn't find source, it's garbled */
803
811
if (* origin_index == -1 ) {
804
812
return NULL ;
805
813
}
806
814
807
- cursor = r -> contents + sizeof (hmac );
808
- max = tal_count (r -> contents ) - sizeof (hmac );
809
815
msglen = fromwire_u16 (& cursor , & max );
810
-
811
- if (msglen > ONION_REPLY_SIZE ) {
812
- return NULL ;
813
- }
814
-
815
- final = tal_arr (ctx , u8 , msglen );
816
- if (!fromwire (& cursor , & max , final , msglen ))
817
- return tal_free (final );
818
- return final ;
816
+ return fromwire_tal_arrn (ctx , & cursor , & max , msglen );
819
817
}
820
818
821
819
struct onionpacket * sphinx_decompress (const tal_t * ctx ,
0 commit comments