Skip to content

Commit d344054

Browse files
committed
p2p: make RLPx frame MAC 16 bytes as defined in the spec
1 parent d084aed commit d344054

2 files changed

Lines changed: 19 additions & 13 deletions

File tree

‎p2p/rlpx.go‎

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,15 @@ func (rw *rlpxFrameRW) WriteMsg(msg Msg) error {
6262
putInt24(fsize, headbuf) // TODO: check overflow
6363
copy(headbuf[3:], zeroHeader)
6464
rw.enc.XORKeyStream(headbuf[:16], headbuf[:16]) // first half is now encrypted
65-
copy(headbuf[16:], updateHeaderMAC(rw.egressMAC, rw.macCipher, headbuf[:16]))
65+
66+
// write header MAC
67+
copy(headbuf[16:], updateMAC(rw.egressMAC, rw.macCipher, headbuf[:16]))
6668
if _, err := rw.conn.Write(headbuf); err != nil {
6769
return err
6870
}
6971

70-
// write encrypted frame, updating the egress MAC while writing to conn.
72+
// write encrypted frame, updating the egress MAC hash with
73+
// the data written to conn.
7174
tee := cipher.StreamWriter{S: rw.enc, W: io.MultiWriter(rw.conn, rw.egressMAC)}
7275
if _, err := tee.Write(ptype); err != nil {
7376
return err
@@ -81,9 +84,10 @@ func (rw *rlpxFrameRW) WriteMsg(msg Msg) error {
8184
}
8285
}
8386

84-
// write packet-mac. egress MAC is up to date because
87+
// write frame MAC. egress MAC hash is up to date because
8588
// frame content was written to it as well.
86-
mac := updateHeaderMAC(rw.egressMAC, rw.macCipher, rw.egressMAC.Sum(nil))
89+
fmacseed := rw.egressMAC.Sum(nil)
90+
mac := updateMAC(rw.egressMAC, rw.macCipher, fmacseed)
8791
_, err := rw.conn.Write(mac)
8892
return err
8993
}
@@ -95,8 +99,8 @@ func (rw *rlpxFrameRW) ReadMsg() (msg Msg, err error) {
9599
return msg, err
96100
}
97101
// verify header mac
98-
shouldMAC := updateHeaderMAC(rw.ingressMAC, rw.macCipher, headbuf[:16])
99-
if !hmac.Equal(shouldMAC[:16], headbuf[16:]) {
102+
shouldMAC := updateMAC(rw.ingressMAC, rw.macCipher, headbuf[:16])
103+
if !hmac.Equal(shouldMAC, headbuf[16:]) {
100104
return msg, errors.New("bad header MAC")
101105
}
102106
rw.dec.XORKeyStream(headbuf[:16], headbuf[:16]) // first half is now decrypted
@@ -115,11 +119,12 @@ func (rw *rlpxFrameRW) ReadMsg() (msg Msg, err error) {
115119

116120
// read and validate frame MAC. we can re-use headbuf for that.
117121
rw.ingressMAC.Write(framebuf)
118-
if _, err := io.ReadFull(rw.conn, headbuf); err != nil {
122+
fmacseed := rw.ingressMAC.Sum(nil)
123+
if _, err := io.ReadFull(rw.conn, headbuf[:16]); err != nil {
119124
return msg, err
120125
}
121-
shouldMAC = updateHeaderMAC(rw.ingressMAC, rw.macCipher, rw.ingressMAC.Sum(nil))
122-
if !hmac.Equal(shouldMAC, headbuf) {
126+
shouldMAC = updateMAC(rw.ingressMAC, rw.macCipher, fmacseed)
127+
if !hmac.Equal(shouldMAC, headbuf[:16]) {
123128
return msg, errors.New("bad frame MAC")
124129
}
125130

@@ -136,14 +141,16 @@ func (rw *rlpxFrameRW) ReadMsg() (msg Msg, err error) {
136141
return msg, nil
137142
}
138143

139-
func updateHeaderMAC(mac hash.Hash, block cipher.Block, header []byte) []byte {
144+
// updateMAC reseeds the given hash with encrypted seed.
145+
// it returns the first 16 bytes of the hash sum after seeding.
146+
func updateMAC(mac hash.Hash, block cipher.Block, seed []byte) []byte {
140147
aesbuf := make([]byte, aes.BlockSize)
141148
block.Encrypt(aesbuf, mac.Sum(nil))
142149
for i := range aesbuf {
143-
aesbuf[i] ^= header[i]
150+
aesbuf[i] ^= seed[i]
144151
}
145152
mac.Write(aesbuf)
146-
return mac.Sum(nil)
153+
return mac.Sum(nil)[:16]
147154
}
148155

149156
func readInt24(b []byte) uint32 {

‎p2p/rlpx_test.go‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ func TestRlpxFrameFake(t *testing.T) {
2929
01010101010101010101010101010101
3030
ba628a4ba590cb43f7848f41c4382885
3131
01010101010101010101010101010101
32-
01010101010101010101010101010101
3332
`)
3433

3534
// Check WriteMsg. This puts a message into the buffer.

0 commit comments

Comments
 (0)