@@ -190,10 +190,16 @@ func (c *Codec) Encode(id enode.ID, addr string, packet Packet, challenge *Whoar
190190 )
191191 switch {
192192 case packet .Kind () == WhoareyouPacket :
193- // just send the WHOAREYOU packet raw again, rather than the re-encoded challenge data
194193 w := packet .(* Whoareyou )
195- if len (w .Encoded ) > 0 {
196- return w .Encoded , w .Nonce , nil
194+ if len (w .ChallengeData ) > 0 {
195+ // This WHOAREYOU packet was encoded before, so it's a resend.
196+ // The unmasked packet content is stored in w.ChallengeData.
197+ // Just apply the masking again to finish encoding.
198+ c .buf .Reset ()
199+ c .buf .Write (w .ChallengeData )
200+ copy (head .IV [:], w .ChallengeData )
201+ enc := applyMasking (id , head .IV , c .buf .Bytes ())
202+ return enc , w .Nonce , nil
197203 }
198204 head , err = c .encodeWhoareyou (id , packet .(* Whoareyou ))
199205 case challenge != nil :
@@ -228,7 +234,6 @@ func (c *Codec) Encode(id enode.ID, addr string, packet Packet, challenge *Whoar
228234 if err != nil {
229235 return nil , Nonce {}, err
230236 }
231- challenge .Encoded = bytes .Clone (enc )
232237 c .sc .storeSentHandshake (id , addr , challenge )
233238 return enc , head .Nonce , err
234239 }
@@ -246,14 +251,10 @@ func (c *Codec) Encode(id enode.ID, addr string, packet Packet, challenge *Whoar
246251
247252// EncodeRaw encodes a packet with the given header.
248253func (c * Codec ) EncodeRaw (id enode.ID , head Header , msgdata []byte ) ([]byte , error ) {
254+ // header
249255 c .writeHeaders (& head )
250-
251- // Apply masking.
252- masked := c .buf .Bytes ()[sizeofMaskingIV :]
253- mask := head .mask (id )
254- mask .XORKeyStream (masked [:], masked [:])
255-
256- // Write message data.
256+ applyMasking (id , head .IV , c .buf .Bytes ())
257+ // message data
257258 c .buf .Write (msgdata )
258259 return c .buf .Bytes (), nil
259260}
@@ -463,7 +464,7 @@ func (c *Codec) Decode(inputData []byte, addr string) (src enode.ID, n *enode.No
463464 // Unmask the static header.
464465 var head Header
465466 copy (head .IV [:], input [:sizeofMaskingIV ])
466- mask := head . mask (c .localnode .ID ())
467+ mask := createMask (c .localnode .ID (), head . IV )
467468 staticHeader := input [sizeofMaskingIV :sizeofStaticPacketData ]
468469 mask .XORKeyStream (staticHeader , staticHeader )
469470
@@ -679,10 +680,17 @@ func (h *StaticHeader) checkValid(packetLen int, protocolID [6]byte) error {
679680}
680681
681682// mask returns a cipher for 'masking' / 'unmasking' packet headers.
682- func ( h * Header ) mask ( destID enode.ID ) cipher.Stream {
683+ func createMask ( destID enode.ID , iv [ 16 ] byte ) cipher.Stream {
683684 block , err := aes .NewCipher (destID [:16 ])
684685 if err != nil {
685686 panic ("can't create cipher" )
686687 }
687- return cipher .NewCTR (block , h .IV [:])
688+ return cipher .NewCTR (block , iv [:])
689+ }
690+
691+ func applyMasking (destID enode.ID , iv [16 ]byte , packet []byte ) []byte {
692+ masked := packet [sizeofMaskingIV :]
693+ mask := createMask (destID , iv )
694+ mask .XORKeyStream (masked [:], masked [:])
695+ return packet
688696}
0 commit comments